I'm trying to use recyclerView in a fragment but whenever i run the app i get a nullpointer exception when i call setLayoutManager on the view. i followed the android documentation on using recyclerview with fragments and looked post related to this problem here but nothing seems to work.
`07-09 17:40:27.784 4565-4565/com.example.broulayedoumbia.restaurantapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.broulayedoumbia.restaurantapp, PID: 4565 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.broulayedoumbia.restaurantapp/com.example.broulayedoumbia.restaurantapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference at com.example.broulayedoumbia.restaurantapp.RestaurantListFragment.onCreateView(RestaurantListFragment.java:79) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
`
Fragment code:
public class RestaurantListFragment extends Fragment implements RestaurantAdapter.ListItemClickListener{
private static final int NUM_LIST_ITEMS = 100;
private OnFragmentInteractionListener mListener;
private RestaurantAdapter mAdapter;
private RecyclerView mRestaurantList;
public RestaurantListFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment RestaurantListFragment.
*/
// TODO: Rename and change types and number of parameters
public static RestaurantListFragment newInstance(String param1, String param2) {
RestaurantListFragment fragment = new RestaurantListFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login, container, false);
/*
* Using findViewById, we get a reference to our RecyclerView from xml. This allows us to
* do things like set the adapter of the RecyclerView and toggle the visibility.
*/
mRestaurantList = (RecyclerView) view.findViewById(R.id.rest_list);
/*
* A LinearLayoutManager is responsible for measuring and positioning item views within a
* RecyclerView into a linear list. This means that it can produce either a horizontal or
* vertical list depending on which parameter you pass in to the LinearLayoutManager
* constructor. By default, if you don't specify an orientation, you get a vertical list.
* In our case, we want a vertical list, so we don't need to pass in an orientation flag to
* the LinearLayoutManager constructor.
*
* There are other LayoutManagers available to display your data in uniform grids,
* staggered grids, and more! See the developer documentation for more details.
*/
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
mRestaurantList.setLayoutManager(layoutManager);
/*
* Use this setting to improve performance if you know that changes in content do not
* change the child layout size in the RecyclerView
*/
mRestaurantList.setHasFixedSize(true);
mAdapter = new RestaurantAdapter(NUM_LIST_ITEMS, this);
mRestaurantList.setAdapter(mAdapter);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onListItemClick(int clickedItemIndex) {
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
}
}
Fragment Xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rest_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Main Activity:
public class MainActivity extends AppCompatActivity implements LoginFragment.OnFragmentInteractionListener, RestaurantListFragment.OnFragmentInteractionListener {
private LoginFragment loginFragment;
private RestaurantListFragment restaurantListFragment;
private FragmentTransaction fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager().beginTransaction();
loginFragment = new LoginFragment();
restaurantListFragment = new RestaurantListFragment();
fragmentManager.replace(R.id.fragContainer, restaurantListFragment).commit();
}
@Override
public void login() {
}
}