Skip to content Skip to sidebar Skip to footer

'boolean Android.support.v7.widget.recyclerview$layoutmanager.canscrollvertically()'

How to fix my issue when I try to use RecyclerView. When I add 2 line of code recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view); recyclerView.setLayoutManager(

Solution 1:

Your recyclerView is null. Because the RecyclerView is only added when fragment_home.xml is loaded to your activity.

That mean it is ready after (assuming your DefaultFragment is inflate fragment_home.xml)

Fragmentfragment=null;
fragment = newDefaultFragment();
FragmentManagerfragmentManager= getSupportFragmentManager();
FragmentTransactionfragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.root, fragment);
fragmentTransaction.commit();

You should do the findViewById after that. However, fragmentTransaction.commit(); is an async process. The fragment content may not be available immediately after that. So, you will need to call fragmentTransaction.commitNow() to make sure the fragment is added to activity.

recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
recyclerView.setLayoutManager(newLinearLayoutManager(this));

It is not a very good idea to find fragment's view from the activity level. One of the reason is to avoid the situation like you are facing now as fragment content cannot be guaranteed in activity.

The better way is to find and set view content inside the fragment onViewCreated

If your data is get from activity, you can still pass the data to fragment from activity.

See, topics like "communication between fragment and activity" https://developer.android.com/training/basics/fragments/communicating.html

Solution 2:

recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
// The above line doesnot throw null pointer exception, since you can cast null to any object. Refer this post - http://stackoverflow.com/questions/18723596/no-exception-while-type-casting-with-a-null-in-java
recyclerView.setLayoutManager(newLinearLayoutManager(this));
// The above line will throw null pointer exception, since here we are accessing the null object and invoking a method on that null object.

Try like this to avoid the null pointer exception

 recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
 if(recyclerView != null) {     
     recyclerView.setLayoutManager(new LinearLayoutManager(this));
     .....
     .....
     .....
 }

Solution 3:

Either move fragment_home.xml code inside your main.xml file, If you want recyclerView inside your activity. i.e.

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><FrameLayoutandroid:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent" ><android.support.v7.widget.RecyclerViewandroid:id="@+id/movies_recycler_view"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="vertical" /></FrameLayout></LinearLayout>

OR

add a Fragment to your activity which inflates fragment_home.xml and move recyclerView initialization code to your fragment. i.e. Below code in your fragment:

recyclerView = (RecyclerView) rootview.findViewById(R.id.movies_recycler_view);
recyclerView.setLayoutManager(newLinearLayoutManager(this));

Post a Comment for "'boolean Android.support.v7.widget.recyclerview$layoutmanager.canscrollvertically()'"