1

I am getting an error saying "No adapter attached; skipping layout", however my images are loading into my LayoutManager just fine. However, when i try to scroll down and load additional data, the application crashes with a NullPointerException. Here is my related code.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    service.getPodcasts()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(podcasts -> mRecyclerView.setAdapter(new PodcastsAdapter(getActivity(), podcasts)));
}

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_podcasts, container, false);

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.podcasts_recycler_view);
    mLayoutManager = new GridLayoutManager(getActivity(), 3);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    //mRecyclerView.setAdapter(mAdapter);

    return rootView;
}

Any ideas on how to fix this?

aestamper
  • 141
  • 1
  • 8
  • can i see code which attatch adapter – sachithkn Jul 19 '15 at 06:22
  • Sorry but I want to know why have you inflated your view inside onCreateView() method you could have directly called setContentView(R.layout.fragment_podcasts) inside your onCreate() method and intialized your recycler view inside onCreate() does it make a difference? – Sanjeev Jul 19 '15 at 06:34
  • remove the commented line. mRecyclerView.setAdapter(mAdapter); i mean remove the // and im assuming mAdapter is already declared and is an instance of RecyclerView.Adapter class – hello_world Jul 19 '15 at 06:36
  • How would removing a commented line help at all? Lmao. What do you mean exactly by using setContentView in onCreate()? – aestamper Jul 19 '15 at 07:22
  • There's nothing wrong with this code. Where is the NPE? It's probably inside your adapter, in which case we'll probably need both the stack trace/logcat output, and the code for the adapter. – Adam S Jul 19 '15 at 12:38
  • The "no adapter attached" message (which is presumably just an error/warning level log?) is because the adapter is attached later, and is not your problem here. You attach a layout manager, the RecyclerView runs through a layout pass, no adapter is attached, it logs that message, carries on. – Adam S Jul 19 '15 at 12:41
  • The NullPointerException occurs in my onBindViewHolder method. I have this line of code "String coverId = channelList.get(position).getEpisode().get(0).getCoverArt();" that is used to load cover art. It loads the art fine initially, but when I scroll down to load more I get the following error: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference. There should not be any more art to load from my server, so it should not be trying to load anything else. – aestamper Jul 19 '15 at 22:54
  • please post your complete code – JAAD Mar 14 '16 at 11:00

1 Answers1

-1

You didn't attach the adapter because you create it after you try to attach it:

In my case:

mRecyclerView.setAdapter(mAdapter); // Here, mAdapter is null
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
piyush poriya
  • 316
  • 1
  • 3
  • 18