【发布时间】:2019-02-25 19:06:47
【问题描述】:
我正在尝试使用 mvvm 架构创建具有视图寻呼机的页面。
所以我想做的是通过使用可观察的方法在第一个选项卡的片段中显示 Recycler 视图。
但是当我尝试设置内容是我片段中的适配器时,我得到了
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.xxxx.view.adapter.FoodListAdapter.setFoodList(java.util.List)' on a null object reference
我的 FragmentStatePagerAdapter 适配器
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Log.d("Food Pager", "get Item");
FoodListFragment foodListFragment = new FoodListFragment();
return foodListFragment;
case 1:
RestaurantListFragment restaurantListFragment2 = new RestaurantListFragment();
return restaurantListFragment2;
case 2:
RestaurantListFragment restaurantListFragment3 = new RestaurantListFragment();
return restaurantListFragment3;
default:
return null;
}
}
我的活动
private void observeViewModel(final CategoryViewModel categoryViewModel) {
// Observe Category Data
categoryViewModel.getCategory().observe(this, new Observer<Category>() {
@Override
public void onChanged(@Nullable Category category) {
// Update UI
if (category != null) {
if (category.foods != null) {
startFoodListFragment(category.foods);
}
} else {
Log.e(TAG, "Category Data is Null");
}
}
});
}
private void startFoodListFragment(List<CategoryQuery.Food> foodList) {
Log.d("startFoodListFragment", "yolo");
FoodListFragment foodListFragment = new FoodListFragment();
foodListFragment.setFoodList(foodList);
}
所以在我的片段中
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.d("Food LIst", "On create VIew");
View rootView = inflater.inflate(R.layout.fragment_food_list, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview_food_list);
// mLoadingIndicator = (ProgressBar) rootView.findViewById(R.id.food_list_loading_indicator);
Integer gridColumns = 2;
Float width = getScreenSize().get("width");
if (width > 600) {
gridColumns = 3;
}
// Create Pinterest Style RecyclerView
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(gridColumns, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
// Cool. it's Very easy ,but margin on my LinearLayout didn’t seem to work. So here’s a quick fix.
SpacesItemDecoration decoration = new SpacesItemDecoration(8);
mRecyclerView.addItemDecoration(decoration);
mFoodListAdapter = new FoodListAdapter(foodListAdapterOnClickHandler);
mRecyclerView.setAdapter(mFoodListAdapter);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (mFoodList != null) {
mFoodListAdapter.setFoodList(mFoodList);
}
}
public void setFoodList(List<CategoryQuery.Food> foodList) {
Log.d(TAG, "setFoodlist");
if (foodList != null) {
mFoodList = foodList;
mFoodListAdapter.setFoodList(mFoodList);
// mFoodListAdapter.notifyDataSetChanged();
}
}
这是我的适配器的一部分
public void setFoodList(final List<? extends CategoryQuery.Food> foodList) {
if (this.foodList == null) {
this.foodList = foodList;
notifyItemRangeInserted(0, foodList.size());
}
}
这是我得到的相关日志
D/Food Pager: get Item
D/Food LIst: On create VIew
D/startFoodListFragment: yolo
D/Food List Fragment: setFoodlist
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
然后错误继续
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.xxx.view.adapter.FoodListAdapter.setFoodList(java.util.List)' on a null object reference
所以任何人都知道为什么我在调用FoodListAdapter 时得到空值,即使在我在onCreateView 内部初始化之后?
感谢您的帮助!
【问题讨论】:
标签: android android-fragments android-recyclerview android-viewpager recyclerview-layout