解决E/RecyclerView: No layout manager attached; skipping layout

简介: 解决E/RecyclerView: No layout manager attached; skipping layout

列表在Android中是最常用的展示方式了,以前主要是使用ListView,现在已经被RecyclerView取代了。

但是用惯了ListView的同学,在用RecyclerView可能习惯了ListView的使用方式。

最常见的错误

//数据源
List list = new ArrayList();
list.add(...);
list.add(...);
list.add(...);
//创建adapter
adapter = new Adapter(list);
//设置adapter
recyclerView.setAdapter(adapter);

运行后,发现列表中没有任何数据,一片空白。。

控制台还会出现以下红色的错误提示。

E/RecyclerView: No layout manager attached; skipping layout

这是因为忘记给RecyclerView添加LayoutManager了。

解决办法。在setAdapter()之前先设置LayoutManager就OK啦。

List list = new ArrayList();
list.add(...);
list.add(...);
list.add(...);
adapter = new Adapter(list);
//必须先设置LayoutManager
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
相关文章
|
1月前
|
开发工具 Android开发
解决Manifest merger failed : android:exported needs to be explicitly specified for <activity>
解决Manifest merger failed : android:exported needs to be explicitly specified for <activity>
40 1
|
11月前
|
Android开发
android 错误 exported needs to be explicitly specified for Apps targeting Android 12
android 错误 exported needs to be explicitly specified for Apps targeting Android 12
196 0
|
Android开发
Android GridLayoutManager.setSpanSizeLookup的使用介绍
Android GridLayoutManager.setSpanSizeLookup的使用介绍
130 0
|
Java API 开发工具
Building Android notifications 2.3 through to 6.0
Building Android notifications 2.3 through to 6.0
58 0
|
XML Android开发 数据格式
【安卓开发】ArrayAdapter requires the resource ID to be a TextView
【安卓开发】ArrayAdapter requires the resource ID to be a TextView
102 0
【安卓开发】ArrayAdapter requires the resource ID to be a TextView
No adapter attached; skipping layout 原因、解决办法
No adapter attached; skipping layout 原因、解决办法
1033 0
|
Android开发
DSL element ‘android.dataBinding.enabled‘ is obsolete and has been replaced with ‘android.buildFeatu
DSL element ‘android.dataBinding.enabled‘ is obsolete and has been replaced with ‘android.buildFeatu
195 0
|
Android开发
Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.C
Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.C
Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.C