android - 我应该如何处理错误 RecyclerView: No adapter attach; 跳过布局
问题描述
我想从 mysql 服务器获取图像到回收站视图。当我运行我的应用程序时。我面临以下问题:
02-25 19:47:47.075 653-12579/? E/WakeLock:GCM_HB_ALARM 释放没有匹配的获取!02-25 19:47:53.241 69-403/? E/FastThread:未收到预期的优先级提升 02-25 19:47:53.393 14523-14523/com.example.wiqarali.tourismapp E/RecyclerView:未连接适配器;跳过布局 02-25 19:47:53.466 14523-14523/com.example.wiqarali.tourismapp E/EventBus:无法调度事件:com.example.wiqarali.tourismapp.serverclasses.GalleryEventBus 类订阅类 com.example。 wiqarali.tourismapp.GalleryActivity java.lang.NullPointerException:尝试在 com 的空对象引用上调用虚拟方法 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' .example.wiqarali.tourismapp.GalleryActivity。
这是我的事件总线代码
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(GalleryEventBus eventBus){
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView_gallery.setLayoutManager(linearLayoutManager);
recyclerView_gallery.setItemAnimator(new DefaultItemAnimator());
GalleryAdapter adapter = new GalleryAdapter(this, eventBus.galleryList);
recyclerView_gallery.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
解决方案
我遇到了和你一样的情况,我刚刚解决了。就我而言,启动时RecyclerView
我只设置适配器如果 data.size() > 0。然后当你收到事件并调用notifyDataSetChanged()
导致该错误
if (data.size() > 0) {
rv.setLayoutManager(new GridLayoutManager(mContext, 3));
rv.setItemAnimator(new DefaultItemAnimator());
rv.setHasFixedSize(true);
rv.setAdapter(myAdapter);
}
推荐阅读
- python - Python DataFrame - 检查其他行是否匹配 ID、类似时间戳
- react-native - react-native-router-flux 导航到同一场景
- c# - Sandcastle 未在 Visual Studio 中记录无库项目
- javascript - 在 webpack 构建期间 Vuetify CSS 更改顺序
- c# - 在另一个应用程序中按下某个键时,如何让我的表单在隐藏时显示?
- java - Tomcat 服务器只是挂起 - log4j 警告
- json - 自动获取 JSON URL 并提交到 git 存储库?
- php - rtrim 函数可以剪切到许多字符
- java - 多个 Java 类中的冗余属性
- python - 用python中可能的字典列表展平嵌套字典