FragmentStatePagerAdapter 在 Glide 中的使用导致 NullPointerException

FragmentStatePagerAdapter usage with Glide results in NullPointerException

一些背景:

我正在使用 FragmentStatePagerAdapter 来实现可变数量片段的水平滑动视图。澄清一下,片段的数量取决于一些外部数据。在这种情况下,碎片的数量可能会很大,我无法使用适配器的 setOffScreenPageLimit() 方法。

现在,进入问题:

我正在使用 Glide 从 Web 检索图像列表并将它们显示在我的片段中。这适用于第一个片段和第二个片段(因为 setOffScreenPageLimit() 默认为 1)。

但是,第 3 个片段超出了 setOffScreenPageLimit() 范围。当我尝试在这个片段中检索图像时,我从一些调用片段上的 getView() 的内部 Glide 方法(这显然是空的)得到一个 NullPointerException。我已经确认这确实是问题所在,例如,当我将屏幕外限制设置为 4 时,当我尝试检索第 5 个片段上的图像时应用程序崩溃。

这是我的 logcat 输出:

 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)

下面是 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();
            }
        }
    }



}

任何人都可以告诉我为什么会发生这种情况吗?该片段出现在屏幕上,UI 的其余部分工作正常(可以按下按钮等)。但是,使用 Glide 加载图像会使应用程序崩溃。我觉得这是某种与 FragmentStatePagerAdapter 如何创建和销毁片段有关的生命周期细节,但我不确定。

非常感谢任何帮助。

我认为Glide.with(this.itemView)这条线有问题。

只需将上下文从 activity 传递到您的适配器并使用它 this.itemView

另一个快速解决方法可能是 Glide.with(mİmageView.getContext())

祝你好运

埃姆雷