某些背景:
我使用FragmentStatePagerAdapter来实现可变数量片段的水平滑动视图。为了澄清,碎片的数量取决于一些外部数据。在这种情况下,片段的数量可能非常大,我不能使用适配器的setOffScreenPageLimit()方法。
现在,关于问题:
我使用Glide从网络中检索图像列表并将其显示在我的片段上。这适用于第一个片段,也适用于第二个片段(因为setOffScreenPageLimit()默认为1)。
但是,第3个片段位于setOffScreenPageLimit()范围之外。当我尝试在此片段中检索图像时,我从一些内部Glide方法获得NullPointerException,该方法在片段上调用getView()(显然为null)。我已经确认这实际上是问题,因为当我将屏幕外限制设置为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类:
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如何创建和销毁碎片有关,但我不确定。
任何帮助非常赞赏。
答案 0 :(得分:1)
我认为Glide.with(this.itemView)
此行存在问题。
只需从活动中将上下文传递给适配器,然后使用它来代替this.itemView
另一种快速解决方法可能是Glide.with(mİmageView.getContext())
埃姆雷