0

I am trying to use RecyclerView and its adapter in a fragment. Whenever I try to open that fragment app crashes and throws an exception:

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> 
4
  • what do you do in initializeGroupsInCommon() method ? Commented Jul 1, 2019 at 6:06
  • why are you declaring inner class as static ? Commented Jul 1, 2019 at 6:11
  • Populating List with dummy data. @VivekMishra Commented Jul 1, 2019 at 6:13
  • The static keyword is the culprit Commented Jul 1, 2019 at 6:27

3 Answers 3

1

Seems that groupsInCommonRecyclerView becomes null somewhere after setting layout manager and before setting adapter. Not obvious from posted code, so try going through your code with debugger step by step.

Sign up to request clarification or add additional context in comments.

Comments

0

getActivity() sometimes return null. So I suggest you to override onAttach() in fragment to get the context. This means your layout manager is null because of null context and is not provided to the recyclerview .So while setting the adapter it throws the exception. Try this and post the update of working status.

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; } } 

Comments

0

I think you have not bind your recyclerview in activity by using findViewById... Please check.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.