在RecyclerView上获取空引用

时间:2019-07-01 05:59:11

标签: android android-recyclerview recycler-adapter

我正在尝试在一个片段中使用RecyclerView及其适配器。每当我尝试打开该片段应用程序时,它就会崩溃并引发异常:

Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget RecyclerView$Adapter)' on a null object reference

MyActivity.java:

public class upperClass extends ... {
    ...
    public static innerClass extends CorrectedPreferenceFragment ... {
         private RecyclerView  groupsInCommonRecyclerView;
         private View          v;    
         ...

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          v = inflater.inflate(R.layout.recipient_common_groups,     container, false);

          groupsInCommonRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
          groupsInCommonRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


          ArrayList<GroupsInCommonItem> commonGroups = initializeGroupsInCommon();

          GroupsInCommonRecycleViewAdapter groupsInCommonRecycleViewAdapter = new GroupsInCommonRecycleViewAdapter(getContext(), commonGroups);

          groupsInCommonRecyclerView.setAdapter(groupsInCommonRecycleViewAdapter);

          return v;
        }          
    }

recipient_common_groups.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <android.support.v7.widget.RecyclerView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/recycler_view">

   </android.support.v7.widget.RecyclerView>

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

似乎在设置布局管理器之后和设置适配器之前,groupsInCommonRecyclerView变为null。从发布的代码中看不出来,所以请尝试逐步使用调试器来遍历代码。

答案 1 :(得分:0)

getActivity()有时返回null。因此,我建议您覆盖片段中的onAttach()以获取上下文。 这意味着您的布局管理器由于上下文为空而为空,并且未提供给recyclerview。因此在设置适配器时会引发异常。 试试这个,并发布工作状态更新。

public class upperClass extends ... {
    ...
    public static innerClass extends CorrectedPreferenceFragment ... {
         private RecyclerView  groupsInCommonRecyclerView;
         private View          v;   
         private Context c;
    @Override
    public void onAttach(Context c) {
        super.onAttach(c);
        this.c = c; //this is one of the best way to get context of the activity to which the particular activity is associated with
      }
  @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          v = inflater.inflate(R.layout.recipient_common_groups,     container, false);

          groupsInCommonRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);

       LinearLayoutManager lm = new LinearLayoutManager(c);
          lm.setOrientation(LinearLayoutManager.VERTICAL);
          groupsInCommonRecyclerView.setLayoutManager(lm);
          ArrayList<GroupsInCommonItem> commonGroups = initializeGroupsInCommon();
          GroupsInCommonRecycleViewAdapter groupsInCommonRecycleViewAdapter = new GroupsInCommonRecycleViewAdapter(c, commonGroups);

          groupsInCommonRecyclerView.setAdapter(groupsInCommonRecycleViewAdapter);

          return v;
        }    
}

答案 2 :(得分:0)

我认为您尚未通过使用findViewById将recyclerview绑定到活动中... 请检查。