我的代码有问题,这是错误的
java.lang.NullPointerException:尝试调用虚拟方法 空对象上的'void android.widget.ImageView.setImageResource(int)' 参考
代码中没有错误,但是运行时会出现null异常错误,我不知道哪里是null,这是错误
在com.mulazi.submission2.MovieListAdapter.onBindViewHolder(MovieListAdapter.java:57) 在com.mulazi.submission2.MovieListAdapter.onBindViewHolder(MovieListAdapter.java:20)
这是我的代码
package com.mulazi.submission2;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.mulazi.submission2.R;
import com.mulazi.submission2.DetailActivity;
import com.mulazi.submission2.Movie;
import java.util.ArrayList;
import java.util.List;
public class MovieListAdapter extends RecyclerView.Adapter<MovieListAdapter.MovieViewHolder> {
private List<Movie> movieList;
public MovieListAdapter() {
movieList = new ArrayList<>();
}
private void add(Movie item) {
movieList.add(item);
notifyItemInserted(movieList.size() - 1);
}
public void addAll(List<Movie> movieList) {
for (Movie movie : movieList) {
add(movie);
}
}
@NonNull
@Override
public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_movie, parent, false);
return new MovieViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MovieViewHolder holder,final int position) {
final Movie movie = movieList.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(holder.itemView.getContext(), DetailActivity.class);
intent.putExtra("key", movie);
holder.itemView.getContext().startActivity(intent);
}
});
holder.moviePhoto.setImageResource(movie.getPhoto());
holder.movieName.setText(movie.getName());
holder.movieDate.setText(movie.getDate());
holder.movieDesc.setText(movie.getDescription());
holder.movieDuration.setText(movie.getDuration());
holder.movieDirector.setText(movie.getDirector());
holder.movieGenre.setText(movie.getGenre());
}
@Override
public int getItemCount() {
return movieList.size();
}
static class MovieViewHolder extends RecyclerView.ViewHolder {
ImageView moviePhoto;
TextView movieName;
TextView movieDesc;
TextView movieDate;
TextView movieDuration;
TextView movieDirector;
TextView movieGenre;
MovieViewHolder(@NonNull View itemView) {
super(itemView);
moviePhoto = itemView.findViewById(R.id.img_photo);
movieName = itemView.findViewById(R.id.txt_name);
movieDesc = itemView.findViewById(R.id.txt_description);
movieDate = itemView.findViewById(R.id.txt_date);
movieDirector = itemView.findViewById(R.id.txt_director);
movieDuration = itemView.findViewById(R.id.txt_duration);
movieGenre = itemView.findViewById(R.id.txt_genre);
}
}
}
我希望有人能帮助我解决此错误,
答案 0 :(得分:0)
检查布局文件。您很可能忘记将img_photo放入布局中:
moviePhoto = itemView.findViewById(R.id.img_photo);