92

I'm trying to mess with the new RecyclerView and whenever I try to run it, my app immediately crashes. It gives me NullPointerException for trying to access methods from android.support.v7.widget.RecyclerView. I've looked at other posts and saw that most people didn't have compile 'com.android.support:recyclerview-v7:+' but I tried that and it hasn't helped at all. Not really sure what to do at this point, any help would be appreciated. Here the error log: (I would post a picture but I don't have 10 rep yet)

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
        at android.view.View.measure(View.java:17430)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17430)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5786)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
l-l
  • 3,804
  • 6
  • 36
  • 42
Joe
  • 1,954
  • 5
  • 16
  • 22
  • for anyone who has this error caused by Viewpager tabs see this [solution](http://stackoverflow.com/a/34500413/2977976) – Hassan Gilak Dec 28 '15 at 20:38
  • Have you solved this ?.Please help I am having same issue – Sagar Nov 28 '17 at 06:58
  • Remember to set/create/register in `onCreateView` what you remove/destroy/unregister in `onDestroyView`... Gotta follow those lifecycles! – Sakiboy Dec 04 '17 at 20:45

22 Answers22

210

This issue usually occurs when no LayoutManager was provided for the RecyclerView. You can do it like so:

final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
Paul Woitaschek
  • 6,717
  • 5
  • 33
  • 52
aga
  • 27,954
  • 13
  • 86
  • 121
  • Thanks, that fixed it. I didn't set layoutManager to final. – Joe Dec 11 '14 at 11:02
  • 14
    I don't think it needs to be final so I don't think that was the problem. In my case I was implementing a fragment with recyclerView and I've tried just to retain a reference to it from onCreateView and set a layoutManager later when the data is loaded. I had this issue and it went away when I've set the layoutManager earlier in the gamein onCreateView method. – Nemanja Kovacevic Dec 31 '14 at 21:45
  • 3
    @NemanjaKovačević well, I suppose that every variable which doesn't need to be changed later has to be declared as `final`, that's why I've declared it `final` here. – aga Jan 01 '15 at 10:52
  • 2
    Since LinearLayoutManager is vertical by default, you could use: recyclerView.setLayoutManager(new LinearLayoutManager(context)); – l-l Feb 25 '15 at 00:25
  • 1
    Thank God that we can Google the very same error messages that are made by Google to make sense out of them. – jhegedus Aug 25 '16 at 10:29
  • My issue is still present. I'm getting null object reference even with this solution. – wesley franks Jun 20 '19 at 13:38
  • 1
    @wesleyfranks it's hard to tell why you're getting null object reference without looking at 1. the code that sets the RecyclerView up and 2. the stacktrace you're getting after the crash. :) – aga Jun 20 '19 at 13:42
  • Thanks, idk about anyone else but I found that taking what I'm trying to do. Put it inside it's own "app" to focus on that feature/function/etc. helps for me to figure out the problem. I'm honestly still not sure what happened, but I've been able to complete what I'm trying to do. Thanks for your response and willingness to assist. – wesley franks Jun 22 '19 at 17:40
26

In my case it was not connected to 'final', but to the issue mentioned in @NemanjaKovačević comment to @aga answer. I was setting a layoutManager on data load and that was the cause of the same crash. After moving the layoutManager setup to onCreateView of my fragment the issue was fixed.

Something like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
{
...
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler);

mLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
YuriK
  • 432
  • 5
  • 13
  • 1
    One more time: I don't tell that this issue is somehow related to variable not being final. The issue is that `LayoutManager` **wasn't provided**, that's it. You can make the variable pointing to `LayoutManager` final, or not final - it doesn't change anything. – aga Jan 13 '15 at 16:15
  • @Yurik i am using recyclerview in Activity not on fragment. what the case for me? – Youri Aug 18 '16 at 08:23
  • @Youri you should just place layoutManager setup into the onCreate of your activity, I think. – YuriK Aug 19 '16 at 15:04
10

For me, I was having the same issue, the issue was that there was an unused RecyclerView in xml with view gone but I am not binding it to any adapter in Activity, hence the issue. It was solved as soon as I removed such unused recycler views in xml

i.e - I removed this view as this was not called in code or any adapter has been set

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv_profileview_allactivities"
        android:visibility="gone" />
JJD
  • 50,076
  • 60
  • 203
  • 339
Maurice
  • 570
  • 6
  • 8
  • Same here everything was perfect, but it was not working. So I removed unused recyclerviews and it worked without any issue. Thanks man @Maurice ^^ – Bhupinder Jul 15 '15 at 08:48
  • 1
    after wasting a day i tried your solution and it worked :D [ :( my time got wasted] – mfaisalhyder Jan 27 '16 at 19:10
9

I experienced this crash even though I had the RecyclerView.LayoutManager properly set. I had to move the RecyclerView initialization code into the onViewCreated(...) callback to fix this issue.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_listing, container, false);
    rootView.setTag(TAG);
    return inflater.inflate(R.layout.fragment_listing, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new ListingAdapter(mListing);
    mRecyclerView.setAdapter(mAdapter);
}
Shirish Kamath
  • 356
  • 4
  • 5
5

You need to use setLayoutManager in the RecyclerView#onCreate() method. Before adding recyclerView to a view, it must have the LayoutManager set.

 private RecyclerView menuAsList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    menuAsList = (RecyclerView) findViewById(R.id.recyclerView_mainMenu);
    menuAsList.setLayoutManager(new LinearLayoutManager(Home.this));

}
JJD
  • 50,076
  • 60
  • 203
  • 339
katwal-Dipak
  • 3,523
  • 2
  • 23
  • 23
3

I got this issue due to wrong reference of RecyclerView id.

recyclerView = (RecyclerView) findViewById(R.id.rv_followers_list);

to

recyclerView = (RecyclerView) findViewById(R.id.rv_search_list);
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Stephen
  • 9,899
  • 16
  • 90
  • 137
2
recyclerView =
            (RecyclerView) findViewById(R.id.recycler_view2);

Check with you recycler view ID, pointing to actual recycler view solved my issue

1

My problem was in my XML lyout I have an android:animateLayoutChanges set to true and I've called notifyDataSetChanged() on the RecyclerView's adapter in the Java code.

So, I've just removed android:animateLayoutChanges from my layout and that resloved my problem.

Ahlem Jarrar
  • 1,129
  • 1
  • 12
  • 33
1

I had this problem when using Butterknife library. I had:

View rootView = inflater.inflate
                (R.layout.fragment_recipe_detail_view, container, false);
ButterKnife.bind(rootView);

But the correct version is:

View rootView = inflater.inflate
                (R.layout.fragment_recipe_detail_view, container, false);
ButterKnife.bind(this, rootView);
IvarsB
  • 53
  • 1
  • 10
1

In Gradle implement this library :

implementation 'com.android.support:appcompat-v7:SDK_VER'
implementation 'com.android.support:design:SDK_VER'
implementation 'com.android.support:support-v4:SDK_VER'
implementation 'com.android.support:support-v13:SDK_VER'
implementation 'com.android.support:recyclerview-v7:SDK_VER'

In XML mention like this :

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

In Fragment we use recyclerView like this :

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View rootView = inflater.inflate(R.layout.YOUR_LAYOUT_NAME, container, false);

       RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        APICallMethod();
        YOURADAPTER = new YOURADAPTER(getActivity(), YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
        recyclerView.setAdapter(YOURADAPTER);
        return rootView;
    }

In Activity we use RecyclerView like this :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.YOUR_LAYOUT_NAME);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        APICallMethod();
        YOURADAPTER = new YOURADAPTER(ACTIVITY_NAME.this, YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
        recyclerView.setAdapter(YOURADAPTER);
   }

if you need horizontal RecyclerView do this :

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
        APICallMethod();
        YOURADAPTER = new YOURADAPTER(ACTIVITY_NAME.this, YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
        recyclerView.setAdapter(YOURADAPTER);

if you want to use GridLayoutManager using RecyclerView use like this :

        RecyclerView recyclerView = findViewById(R.id.rvNumbers);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); // 3 is number of columns.
        adapter = new MyRecyclerViewAdapter(this, data);
        recyclerView.setAdapter(adapter);
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
1

I was using a Dependency Injection - ButterKnife and this problem arised because I had not binded the view.

So inside onCreate() I just inserted this line:

ButterKnife.bind(this);

My RecyclerView declaration with Dependency Injection:

@BindView(R.id.recyclerview)
RecyclerView recyclerView;
rohegde7
  • 633
  • 9
  • 15
1

For those who are using: implementation 'androidx.recyclerview:recyclerview:1.1.0-beta05'

make sure you are using this in your xml layout

I also experienced a crash when running my app below API 27. Here's a link to the message

Originally, I have created a style to change the font size of my textview inside the recyclerview that's why I created this code below: (I could have just called @dimen when changing the font size based on screen sizes)

Inside style.xml

and added this in my theme

themes.xml

After investigating, I found out that this is the one causing me an error and after removing the style, I no longer experiencing a crash.

I tried all the possible solutions I found here in stack overflow and almost gave up on this, thankfully in my curiosity I thought why not removing the style maybe this causes the crash and then boom the crash is gone.

Chris B
  • 925
  • 4
  • 14
Jeanne vie
  • 518
  • 6
  • 12
  • 1
    Please include code examples directly rather than linking to images. Take a look at the "Help others reproduce the problem" section: https://stackoverflow.com/help/how-to-ask – Chris B Oct 17 '19 at 08:42
  • 1
    I'm really for this @ChrisB. This is my first time putting a comment here as my reputation is too low as you can see. I encountered the crash today and I was thinking to post here as maybe there are people who are also facing this especially they are using 'androidx.recyclerview:recyclerview:1.1.0-beta05'. – Jeanne vie Oct 17 '19 at 09:50
0

Since LinearLayoutManager is vertical by default, an easier way to do this is:

recyclerView.setLayoutManager(new LinearLayoutManager(context));

If you want to change the orientation you could use this constructor:

public LinearLayoutManager(Context context, int orientation, boolean reverseLayout);
l-l
  • 3,804
  • 6
  • 36
  • 42
0

I think the problem in your Adapter. Make sure you have returned ViewHolder in onCreateViewHolder(). Like below:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v;
    v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_leaderboard, parent, false);
    ViewHolder view_holder = new ViewHolder(v);
    return view_holder;
}
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Android Dev
  • 1,496
  • 1
  • 13
  • 25
0

For me the problem was with my activity_main.xml(21) in which the recycleView didn't have an id.

activity_main.xml(21)

 <android.support.v7.widget.RecyclerView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button3"
    tools:listitem="@layout/transaction_list_row" />

activity_main.xml

<android.support.v7.widget.RecyclerView

    android:id="@+id/transac_recycler_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button3"
    />

It worked when i added a android:id="@+id/transac_recycler_view" to the activity_main.xml(21) recycleView

David Kathoh
  • 191
  • 1
  • 9
0

In my case, I added only butterknife library and forget to add annotationProcessor. By adding below line to build.gradle (App module), solved my problem.

    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
Pooja
  • 475
  • 5
  • 14
0

Recyclerview recylerview = findbyid(R.id.recyclerview) May be you have missed it

Sadique Khan
  • 230
  • 3
  • 9
  • Hi, welcome to SO! Please explain some more why this is relevant for this question. As it is currently written it's hard to say what you are refering to. – Piotr Kamoda Jul 25 '19 at 05:51
0

I had similar problem when I test my application on GenyMotion. After restart the device in GenyMotion problem was solved

0

I faced the same issue cause I had the RecyclerView in another layout and not on activity_main.xml.

By searching on some sites I got something and felt like sharing here

  1. Add Id to Include tag in Activity_main.xml

    <include id="includedRecyclerLayout" .... </include>

  2. call this layout before Searching view by ID in MainActivity.java

    `ConstraintLayout includedLayout = findViewById(R.id.inckudedRecyclerLayout);

    RecyclerViewrecyclerview=includedLayout.findViewById(R.id.RECYCLER_VIEW_ID)`;

Nupoor
  • 1
  • 2
0

I had this problem when using Butterknife , I was using fragment

For Fragment, it should be ButterKnife.bind(this,view);

For Activity ButterKnife.bind(this);

Anjana
  • 37
  • 9
-1

If I where you I will do this. onView(withId(android.R.id.list)).perform(RecyclerViewActions.scrollToPosition(3)); android.R.id.list change it to your list id and position will be your position inside your array.

keti
  • 21
  • 4
-1

using fragment

writing code in onCreateView()

RecyclerView recyVideo = view.findViewById(R.id.recyVideoFag);

using activity

writing code in onCreate() RecyclerView recyVideo = findViewById(R.id.recyVideoFag);

sajid
  • 179
  • 1
  • 6