I am getting this specific error during the runtime of my android app:
java.lang.NullPointerException: Attempt to invoke virtual method`'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at com.monkeyengineer.project.MyFragment.onCreateView(MyFragment.java:36)
I have checked the android recycler view and adapter documentation but it is not helping me with why this is crashing.
Here is my code in my Fragment where I am calling the findViewById
method on the container view (import statements are omitted for brevity):
public class MyFragment extends Fragment {
private List<Project> data;
private RecyclerView recyclerView;
private TextView textView;
private boolean mIsRecyclerViewVisible = true;
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.my_fragment, container, false);
textView = (TextView) container.findViewById(R.id.text_view);
recyclerView = (RecyclerView) container.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(mLayoutManager);
Adapter adapter = new ProjectsAdapter(data);
recyclerView.setAdapter(adapter);
if (data != null) {
textView.setVisibility(View.INVISIBLE);
mIsRecyclerViewVisible = true;
} else {
recyclerView.setVisibility(View.INVISIBLE);
mIsRecyclerViewVisible = false;
}
return mView;
}
@Override
public void onStart() {
super.onStart();
if (mIsRecyclerViewVisible) {
recyclerView.setVisibility(View.VISIBLE);
textView.setVisibility(View.INVISIBLE);
} else {
recyclerView.setVisibility(View.INVISIBLE);
textView.setVisibility(View.VISIBLE);
}
}
@Override
public void onStop() {
super.onStop();
mIsRecyclerViewVisible = data != null;
}
}
Here is my fragment layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.monkeyengineer.project.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="45dp"
android:drawableLeft="@android:drawable/ic_input_add"
android:drawableStart="@android:drawable/ic_input_add"
android:drawablePadding="8dp"
android:gravity="start|center"
android:padding="8dp"
android:text="Add stuff."
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
So why is it giving a null pointer exception, it must be because the recycler view object is being assigned to a null value but I can not think why.
Thanks.