gpt4 book ai didi

java - FragmentStatePagerAdapter 与 Glide 一起使用会导致 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 13:03:29 24 4
gpt4 key购买 nike

一些背景:

我使用 FragmentStatePagerAdapter 来实现可变数量 fragment 的水平滑动 View 。需要澄清的是, fragment 的数量取决于一些外部数据。由于这种情况,并且 fragment 的数量可能非常大,因此我无法使用适配器的 setOffScreenPageLimit() 方法。

现在,解决问题:

我正在使用 Glide 从网络检索图像列表并将它们显示在我的 fragment 上。这对于第一个 fragment 和第二个 fragment 都适用(因为 setOffScreenPageLimit() 默认为 1)。

但是,第三个 fragment 超出了 setOffScreenPageLimit() 范围。当我尝试检索此 fragment 中的图像时,我从某个调用 fragment 上的 getView() 的内部 Glide 方法中收到 NullPointerException(显然为 null)。我已经确认这实际上是问题所在,例如,当我将离屏限制设置为 4 时,当我尝试检索第 5 个 fragment 上的图像时,应用程序崩溃了。

这是我的 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 类:

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



}

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

非常任何帮助。

最佳答案

我认为Glide.with(this.itemView)这一行有问题。

只需将上下文从 Activity 传递到适配器并使用它来代替 this.itemView

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

祝你好运

埃姆雷

关于java - FragmentStatePagerAdapter 与 Glide 一起使用会导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44231335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com