Search code examples
javaandroidandroid-fragmentsandroid-glidefragmentstatepageradapter

FragmentStatePagerAdapter usage with Glide results in NullPointerException


Some background:

I'm using a FragmentStatePagerAdapter in order to implement a horizontal swipe-view of a variable amount of fragments. To clarify, the amount of fragments depends on some external data. As this is the case, and the amount of fragments can be quite large, I cannot use the setOffScreenPageLimit() method of the adapter.

Now, on to the problem:

I'm using Glide to retrieve a list of images from the web and display them on my fragment. This works fine for the first fragment, and also the second (as setOffScreenPageLimit() defaults to 1).

However, the 3rd fragment is outside the setOffScreenPageLimit() range. When I try to retrieve images at this fragment, I get a NullPointerException from some interal Glide method that calls getView() on the fragment (which is apparently null). I've confirmed that this is actually the problem, as when I set the off-screen limit to 4, for example, the the application crashes when I try to retrieve the images on the 5th fragment.

This is my logcat output:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v4.app.Fragment.getView()' on a null object reference
                                                                          at com.bumptech.glide.manager.RequestManagerRetriever.findAllSupportFragmentsWithViews(RequestManagerRetriever.java:189)
                                                                          at com.bumptech.glide.manager.RequestManagerRetriever.findSupportFragment(RequestManagerRetriever.java:200)
                                                                          at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:167)
                                                                          at com.bumptech.glide.Glide.with(Glide.java:609)
                                                                          at com.android.miki.quickly.GIFRecyclerAdapter.onViewRecycled(GIFRecyclerAdapter.java:51)
                                                                          at com.android.miki.quickly.GIFRecyclerAdapter.onViewRecycled(GIFRecyclerAdapter.java:17)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.dispatchViewRecycled(RecyclerView.java:6056)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.addViewHolderToRecycledViewPool(RecyclerView.java:5827)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:5792)
                                                                          at android.support.v7.widget.RecyclerView$LayoutManager.scrapOrRecycleView(RecyclerView.java:8462)
                                                                          at android.support.v7.widget.RecyclerView$LayoutManager.detachAndScrapAttachedViews(RecyclerView.java:8447)
                                                                          at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
                                                                          at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
                                                                          at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
                                                                          at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
                                                                          at android.view.View.layout(View.java:17637)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                          at android.view.View.layout(View.java:17637)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                          at android.view.View.layout(View.java:17637)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1795)
                                                                          at android.view.View.layout(View.java:17637)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                          at android.view.View.layout(View.java:17637)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                          at android.view.View.layout(View.java:17637)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                          at android.view.View.layout(View.java:17637)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                          at android.view.View.layout(View.java:17637)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                          at android.view.View.layout(View.java:17637)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                          at android.view.View.layout(View.java:17637)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)

The following is the GIFRecyclerAdapter class:

  public class GIFRecyclerAdapter extends RecyclerView.Adapter<GIFRecyclerAdapter.GifViewHolder>{


    private List<String> gifURLs;

    public GIFRecyclerAdapter(List<String> gifURLs) {
        this.gifURLs = gifURLs;
    }

    @Override
    public GifViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_gif, parent, false);
        GifViewHolder vh = new GifViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(GifViewHolder holder, int position) {
        holder.setGif(gifURLs.get(position));
    }

    @Override
    public int getItemCount() {
        return gifURLs.size();
    }

    public void setURLs(List<String> gifURLs) {
        this.gifURLs = gifURLs;
    }

    @Override
    public void onViewRecycled(GifViewHolder holder){
        super.onViewRecycled(holder);
        Glide.with(holder.itemView).clear(holder.mImageViewGif);
    }


    protected class GifViewHolder extends RecyclerView.ViewHolder {

        private ImageView mImageViewGif;

        private GifViewHolder(View itemView) {
            super(itemView);
            mImageViewGif = (ImageView) itemView.findViewById(R.id.gif_image_view);
        }

        private void setGif(String gifURL) {
            try{
                Glide.with(this.itemView).load(gifURL).into(mImageViewGif);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }



}

Can anyone give me some tips as to why this is happening? The fragment appears on the screen, and the rest of the UI works ok (buttons can be pressed, etc). However, loading images with Glide crashes the app. I have a feeling it's some sort of lifecycle detail having to do with how FragmentStatePagerAdapter creates and destroys fragments, but I'm not sure.

Any help is greatly appreciated.


Solution

  • I think Glide.with(this.itemView) this line have problem.

    Just pass context to your adapter from activity and use it instead this.itemView

    Another fast workaround might be Glide.with(mİmageView.getContext())

    Good luck

    Emre