在CardView中设置ImageView

时间:2016-10-13 20:40:16

标签: android picasso android-cardview

正如标题所说,我试图设置一个位于CardView内的ImageView的源代码。但是,我反复收到一个nullpointer异常,我不知道为什么。这就是我的适配器的样子:

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

完整错误日志:

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)

1 个答案:

答案 0 :(得分:1)

您必须确保将正确的ID传递给ImageView

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

或者你在这里正在膨胀正确的布局文件

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