首页 > 解决方案 > 我应该如何处理错误 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();
}

标签: android

解决方案


我遇到了和你一样的情况,我刚刚解决了。就我而言,启动时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);
  }

推荐阅读