user11230 user11230 - 2 months ago 118
Android Question

Glide error when loading a recyclerView "You must pass in a non null View"

Hi I am trying to populate a Grid RecyclerView with a series of Images.
To do that I fetch image urls from the web and load them into a List<>. The problem is that apparently the ImageView reference that the adapter gets is null. This is the code:

private class ShowHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public ProgressBar progressBar;

public ShowHolder(LayoutInflater inflater, ViewGroup container) {
super(inflater.inflate(R.layout.list_item_row, container, false));
mImageView = (ImageView) itemView.findViewById(R.id.imageView);
progressBar = (ProgressBar) itemView.findViewById(R.id.progress);
}
}

private class ShowAdapter extends RecyclerView.Adapter<ShowHolder> {
private List<Show> mShows;

public ShowAdapter(List<Show> shows) {
mShows = shows;
}

@Override
public ShowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
return new ShowHolder(layoutInflater, parent);
}
@Override
public void onBindViewHolder(ShowHolder holder, int position) {
Show currentShow = mShows.get(position);




Glide.with(getActivity())
.load(currentShow.getImage()) //image url
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
holder.progressBar.setVisibility(View.GONE);
return false;
}
})
.into(holder.mImageView); //error here

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


This is how I pass the image urls to the adapter:

@Override
public void onLoadFinished(Loader<List<Show>> loader, List<Show> shows) {
// Clear the adapter of previous earthquake data
mShowAdapter = new ShowAdapter(null);
mShowAdapter.notifyDataSetChanged();

// If there is a valid list of Shows, then add them to the adapter's
// data set. This will trigger the ListView to update.
Log.v(LOG_TAG, "onLoadFinished@shows: " + shows);
if (shows != null && !shows.isEmpty()) {
mShowAdapter = new ShowAdapter(shows);
mShowAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mShowAdapter);
}

pb.setVisibility(View.GONE);
}


Stacktrace:

FATAL EXCEPTION: main
Process: com.dcs.shows, PID: 27818
java.lang.IllegalArgumentException: You must pass in a non null View
at com.bumptech.glide.GenericRequestBuilder.into(GenericRequestBuilder.java:678)
at com.bumptech.glide.DrawableRequestBuilder.into(DrawableRequestBuilder.java:448)
at com.dcs.shows.ListFragment$ShowAdapter.onBindViewHolder(ListFragment.java:154)
at com.dcs.shows.ListFragment$ShowAdapter.onBindViewHolder(ListFragment.java:118)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:531)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1319)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:817)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1191)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)


Thanks

Answer

Change your onCerateViewHolder method to:

    @Override
    public ShowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            View rootView = inflater.inflate(R.layout.list_item_row, parent, false);
            return new ShowHolder(rootView);
    }

And change your holder constructor to:

   public ShowHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.imageView);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progress);
   }
Comments