如何解决 getView() 在主页片段活动中的片段中的空指针异常?


public class HomeFragment extends Fragment implements HomeView{
private HomeViewModel homeViewModel;
public static final String EXTRA_CATEGORY = "category";
public static final String EXTRA_POSITION = "position";
public static final String EXTRA_DETAIL = "detail";
@BindView(R.id.viewPagerHeader)
ViewPager viewPagerMeal;
@BindView(R.id.recyclerCategory)
RecyclerView recyclerViewCategory;
HomePresenter presenter;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
super.onCreate(savedInstanceState);
ButterKnife.bind(getActivity());
presenter = new HomePresenter(this);
presenter.getMeals();
presenter.getCategories();
return root;
}
@Override
public void showLoading() {
getView().findViewById(R.id.shimmerMeal).setVisibility(View.VISIBLE);
getView().findViewById(R.id.shimmerCategory).setVisibility(View.VISIBLE);
}
@Override
public void hideLoading() {
getView().findViewById(R.id.shimmerMeal).setVisibility(View.GONE);
getView().findViewById(R.id.shimmerCategory).setVisibility(View.GONE);
}
@Override
public void setMeal(List<Meals.Meal> meal) {
ViewPagerHeaderAdapter headerAdapter = new ViewPagerHeaderAdapter(meal, getActivity());
viewPagerMeal.setAdapter(headerAdapter);
viewPagerMeal.setPadding(20, 0, 150, 0);
headerAdapter.notifyDataSetChanged();
headerAdapter.setOnItemClickListener((view, position) -> {
TextView mealName = view.findViewById(R.id.mealName);
Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class);
intent.putExtra(EXTRA_DETAIL,mealName.getText().toString());
startActivity(intent);
});
}
@Override
public void setCategory(List<Categories.Category> category) {
RecyclerViewHomeAdapter homeAdapter = new RecyclerViewHomeAdapter(category, getActivity());
recyclerViewCategory.setAdapter(homeAdapter);
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3,
GridLayoutManager.VERTICAL, false);
recyclerViewCategory.setLayoutManager(layoutManager);
recyclerViewCategory.setNestedScrollingEnabled(true);
homeAdapter.notifyDataSetChanged();
homeAdapter.setOnItemClickListener((view, position) -> {
Intent intent = new Intent(getActivity(), CategoryActivity.class);
intent.putExtra(EXTRA_CATEGORY, (Serializable) category);
intent.putExtra(EXTRA_POSITION, position);
startActivity(intent);
});
}
@Override
public void onErrorLoading(String message) {
Utils.showDialogMessage(getActivity(), "Title", message);
}

} 这是fragment_home.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment"
android:fitsSystemWindows="true">
<android.support.v4.widget.NestedScrollView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground">
<RelativeLayout
android:id="@+id/headerView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp">
<View
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="@color/colorPrimary" />
<TextView
android:id="@+id/title"
android:textColor="@color/colorWhite"
android:text="@string/app_name"
android:textStyle="bold"
android:textSize="30sp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.CardView
android:id="@+id/cardSearch"
android:layout_below="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="15dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
app:cardCornerRadius="10dp"
app:cardElevation="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="10dp"
android:text="@string/search_your_recipes"
android:textColor="@color/colorPrimaryLight"
android:gravity="center_vertical"
android:drawableLeft="@drawable/ic_search_primary_light"
android:drawableStart="@drawable/ic_search_primary_light"
android:drawablePadding="10dp"/>
</android.support.v7.widget.CardView>
<android.support.v4.view.ViewPager
android:id="@+id/viewPagerHeader"
android:foregroundGravity="center"
android:overScrollMode="never"
android:clipToPadding="false"
android:layout_below="@id/cardSearch"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="150dp" />
<include android:id="@+id/shimmerMeal"
layout="@layout/item_view_pager_header_shimmer" />
<TextView
android:id="@+id/titleCategory"
android:text="@string/meal_categories"
android:textSize="19sp"
android:textColor="@color/colorPrimary"
android:textStyle="bold"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/viewPagerHeader"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerCategory"
android:scrollbars="none"
android:layout_marginTop="10dp"
android:clipToPadding="false"
android:layout_below="@id/titleCategory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="50dp" />
<include android:id="@+id/shimmerCategory"
layout="@layout/item_recycler_category_shimmer" />
<TextView
android:padding="10dp"
android:gravity="center"
android:textColor="@color/colorWhite"
android:text="@string/app_name"
android:background="@color/colorPrimaryLight"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

showloading()hideloading()Java.lang.NullPointerException中的getView().findViewById()出错。
如何解决此错误?到目前为止,尝试了很多方法来解决这个问题? 查看了许多关于此的StackOverflow概念,我不知道如何使其得到纠正。我还在fragmment_home.xml中加入了微光粉和容器,如上面的xml代码所示。

在getView之前检查此命令是否添加了片段

if(isAdded()){
getView().findViewById(R.id.shimmerMeal).setVisibility(View.VISIBLE);
}

getView()方法返回null,因为它是在onCreateView()方法完成之前调用的。

我的建议是将您需要的 UI 小部件存储为类字段onCreateView()

shimmerMeal = root.findViewById(R.id.shimmerMeal);
shimmerCategory = root.findViewById(R.id.shimmerCategory);

然后,可以按如下方式更改showLoading()hideLoading()

@Override
public void showLoading() {
shimmerMeal.setVisibility(View.VISIBLE);
shimmerCategory.setVisibility(View.VISIBLE);
}
@Override
public void hideLoading() {
shimmerMeal.setVisibility(View.GONE);
shimmerCategory.setVisibility(View.GONE);
}

最新更新