0

I want to fetch images from mysql server into recycler view. When I run my app. I face the following problem:

02-25 19:47:47.075 653-12579/? E/WakeLock: GCM_HB_ALARM release without a matched acquire! 02-25 19:47:53.241 69-403/? E/FastThread: did not receive expected priority boost 02-25 19:47:53.393 14523-14523/com.example.wiqarali.tourismapp E/RecyclerView: No adapter attached; skipping layout 02-25 19:47:53.466 14523-14523/com.example.wiqarali.tourismapp E/EventBus: Could not dispatch event: class com.example.wiqarali.tourismapp.serverclasses.GalleryEventBus to subscribing class class com.example.wiqarali.tourismapp.GalleryActivity java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference at com.example.wiqarali.tourismapp.GalleryActivity.onMessageEvent(GalleryActivity.java:131) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:507) at org.greenrobot.eventbus.EventBus.postToSubscription(EventBus.java:434) at org.greenrobot.eventbus.EventBus.postSingleEventForEventType(EventBus.java:411) at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:384) at org.greenrobot.eventbus.EventBus.post(EventBus.java:265) at com.example.wiqarali.tourismapp.serverclasses.GetImages$1.onResponse(GetImages.java:59) at com.example.wiqarali.tourismapp.serverclasses.GetImages$1.onResponse(GetImages.java:37) at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82) at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135)

Here is my event bus code

@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();
}
Mikev
  • 2,012
  • 1
  • 15
  • 27
Wiqar Ali
  • 1
  • 4

1 Answers1

0

I faced the same situation as you and I've just fixed it. In my case, when initiate RecyclerView I only set adapter if data.size() > 0. Then after that when you receive event and call notifyDataSetChanged() causing that error

  if (data.size() > 0) {
            rv.setLayoutManager(new GridLayoutManager(mContext, 3));
            rv.setItemAnimator(new DefaultItemAnimator());
            rv.setHasFixedSize(true);
            rv.setAdapter(myAdapter);
  }
Hai Hack
  • 948
  • 13
  • 24