Android RecyclerView LayoutManager Exception

最后都变了- 提交于 2019-12-05 16:04:09

问题


Hi I'm having an issue with the new Android Lollipop RecyclerView

It keeps crashing with the following exception:

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

Full stack trace can found here

The code used to initiate the RecyclerView is the same mostly the same as the tutorial, however for some reason this still complains. Any help would be greatly appreciated.

The code can be accessed at: gist here and layout: this gist

None of the views, or objects are null


回答1:


Add the following code:

    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);

Before adding adapter:

recyclerView.setAdapter(adapter);



回答2:


In the android Api level 22 it will not give the error. Instead it skips the layout if the Adapter is not found.

By showing the warning:

RecyclerView﹕ No adapter attached; skipping layout

But your app will not crash and runs, but recyclerView doesn't shown.



来源:https://stackoverflow.com/questions/26446162/android-recyclerview-layoutmanager-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!