[英]Android RecyclerView LayoutManager Exception
Hi I'm having an issue with the new Android Lollipop RecyclerView
嗨,我遇到了新的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. 用于启动RecyclerView
的代码与教程大致相同,但由于某种原因,这仍然是抱怨。 Any help would be greatly appreciated. 任何帮助将不胜感激。
The code can be accessed at: gist here and layout: this gist 可以在以下位置访问代码: gist here和layout: this gist
None of the views
, or objects
are null
views
或objects
都不为null
Add the following code: 添加以下代码:
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
Before adding adapter: 添加适配器之前 :
recyclerView.setAdapter(adapter);
In the android Api level 22 it will not give the error. 在Android Api级别22,它不会给出错误。 Instead it skips the layout if the Adapter is not found. 相反,如果找不到适配器,它会跳过布局。
By showing the warning: 通过显示警告:
RecyclerView﹕ No adapter attached; RecyclerView:没有连接适配器; skipping layout 跳过布局
But your app will not crash and runs, but recyclerView
doesn't shown. 但是你的应用程序不会崩溃并运行,但recyclerView
没有显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.