RecyclerView.Adapter return 带有 viewPager 的 Fragment 中的空对象引用
RecyclerView.Adapter return null object reference inside Fragment with viewPager
我正在尝试创建具有 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;
}
}
我的activity
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
时得到 null,甚至在我在 onCreateView
中初始化之后?
感谢您的帮助!
在您的 activity 中,您有此方法 startFoodListFragment()
,它创建一个片段并在其上调用 setFoodList()
方法,该方法在内部将数据设置到适配器。
由于适配器实例是在 onCreateView()
生命周期方法上创建的,因此在创建对象时不会初始化适配器。所以那时它将为空,这就是它抛出 NullPointerException
的原因。确保你访问adapter的时候,一定是在onCreateView()
方法执行之后,这样你就再也不会得到NPE
了!
日志告诉我们什么?
D/Food Pager: get Item
FragmentStatePagerAdapter.getItem(int position)
被调用以显示 FoodListFragment
。这通常意味着 Fragment
将被添加到屏幕上,因此接下来应该调用它的 onCreateView()
。实际情况是这样的:
D/Food LIst: On create VIew
另一方面,我们有一个 Observer
来监听数据变化。每次触发时,都会调用 startFoodListFragment()
:
D/startFoodListFragment: yolo
在此方法中,将创建 FoodListFragment
的新实例。您尝试通过调用 setFoodList()
将新数据传递给它
D/Food List Fragment: setFoodlist
但是由于这个新实例不会被附加到屏幕上,所以它的 onCreateView()
还没有被调用。所以它的适配器还没有被实例化。这就是应用程序崩溃的原因:
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
你能做什么?
您应该将数据传递到 FoodListFragment
的正确实例中。或者简单地在 FoodListFragment
中注册 Observer
。 Fragment
和 Activity
一样都是生命周期所有者,所以我认为这种方法最适合你。
我正在尝试创建具有 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;
}
}
我的activity
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
时得到 null,甚至在我在 onCreateView
中初始化之后?
感谢您的帮助!
在您的 activity 中,您有此方法 startFoodListFragment()
,它创建一个片段并在其上调用 setFoodList()
方法,该方法在内部将数据设置到适配器。
由于适配器实例是在 onCreateView()
生命周期方法上创建的,因此在创建对象时不会初始化适配器。所以那时它将为空,这就是它抛出 NullPointerException
的原因。确保你访问adapter的时候,一定是在onCreateView()
方法执行之后,这样你就再也不会得到NPE
了!
日志告诉我们什么?
D/Food Pager: get Item
FragmentStatePagerAdapter.getItem(int position)
被调用以显示 FoodListFragment
。这通常意味着 Fragment
将被添加到屏幕上,因此接下来应该调用它的 onCreateView()
。实际情况是这样的:
D/Food LIst: On create VIew
另一方面,我们有一个 Observer
来监听数据变化。每次触发时,都会调用 startFoodListFragment()
:
D/startFoodListFragment: yolo
在此方法中,将创建 FoodListFragment
的新实例。您尝试通过调用 setFoodList()
D/Food List Fragment: setFoodlist
但是由于这个新实例不会被附加到屏幕上,所以它的 onCreateView()
还没有被调用。所以它的适配器还没有被实例化。这就是应用程序崩溃的原因:
D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main
你能做什么?
您应该将数据传递到 FoodListFragment
的正确实例中。或者简单地在 FoodListFragment
中注册 Observer
。 Fragment
和 Activity
一样都是生命周期所有者,所以我认为这种方法最适合你。