SpecialSnowflake SpecialSnowflake - 1 month ago 26
Android Question

Setting ImageView inside CardView

So as the title says, I'm trying to set the source of an ImageView which lies inside a CardView. However, I am repeatedly receiving a nullpointer exception and I'm not sure why. This is what my adapter looks like:

public class MovieAdapter extends CursorRecyclerViewAdapter<MovieAdapter.ViewHolder>{

public MovieAdapter(Context context, Cursor cursor) {
super(context, cursor);
}


public static class ViewHolder extends RecyclerView.ViewHolder{
public CardView poster;
public ImageView poster_movie;
public ViewHolder(View view){
super(view);
poster = (CardView) view.findViewById(R.id.card_movie);
poster_movie = (ImageView) view.findViewById(R.id.card_movie_poster);
}
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater
.from(parent.getContext()).inflate(R.layout.row, parent, false);
ViewHolder viewHolder = new ViewHolder(itemView);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {
String posterPath = cursor.getString(cursor.getColumnIndexOrThrow("posterPath"));
Picasso.with(super.getContext()).load("http://image.tmdb.org/t/p/w185//"+ posterPath).into(viewHolder.poster_movie);
}
}


Full error log:

java.lang.IllegalArgumentException: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
at com.example.jay.udacitypopularmovies.MovieAdapter.onBindViewHolder(MovieAdapter.java:49)
at com.example.jay.udacitypopularmovies.MovieAdapter.onBindViewHolder(MovieAdapter.java:21)
at com.example.jay.udacitypopularmovies.adapters.CursorRecyclerViewAdapter.onBindViewHolder(CursorRecyclerViewAdapter.java:80)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5825)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5094)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:541)
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:3315)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3568)

Answer

You have to make sure that you are passing the right id to your ImageView here

poster_movie = (ImageView) view.findViewById(R.id.card_movie_poster);

or you are inflating the right layout file here

View itemView = LayoutInflater
        .from(parent.getContext()).inflate(R.layout.row, parent, false);
Comments