Rendering Problems: java.lang.NullPointerException at android.support.v7.widget.RecyclerView in Android Studio 1.1.0

后端 未结 6 792
忘掉有多难
忘掉有多难 2020-12-31 03:32

I have recently updated android sdk to api 22 and android studio 1.1.0. After that I am getting rendering issues on RecyclerView. Here is what I am getting

          


        
6条回答
  •  余生分开走
    2020-12-31 04:00

    I have had the same error. line 1216 of RecyclerView is:

    return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollRange(mState) : 0;
    

    mLayout being null at the time is the issue, I am not sure why it is null, but it appears you have to call setLayoutManager() on your RecyclerView immediately after setContentView() or inflate(). My code was running a background thread before I tried to access the RecyclerView and call its setLayoutManager(). Once I changed to the following, it works

    i.e.

    this.setContentView(R.layout.schedule);
    rv = (RecyclerView) this.findViewById(R.id.schedule_listview);
    rv.setLayoutManager(new LinearLayoutManager(this));
    

    I am still not quite understand why have to do that though

提交回复
热议问题