我正在使用CardView和RecyclerView。当我添加此行“ listener.onCardSelected(position,holder.thumbnail);”时在侦听器中,我得到了错误。如果我在这行上打断我的项目,请运行。但是我需要这条线,而且我找不到它的固定方式, 抱歉,我的英语
public class AlbumsAdapter extends RecyclerView.Adapter<AlbumsAdapter.MyViewHolder> {
private Context mContext;
private List<Album> albumList;
private AlbumsAdapterListener listener;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, count;
public ImageView thumbnail, overflow;
public CardView cardView;
public MyViewHolder(View view) {
super(view);
title = view.findViewById(R.id.title);
count = view.findViewById(R.id.count);
thumbnail = view.findViewById(R.id.thumbnail);
overflow = view.findViewById(R.id.overflow);
cardView = view.findViewById(R.id.card_view);
}
}
public AlbumsAdapter(Context mContext, List<Album> albumList, AlbumsAdapterListener listener) {
this.mContext = mContext;
this.albumList = albumList;
this.listener = listener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.album_card, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
Album album = albumList.get(position);
holder.title.setText(album.getName());
holder.count.setText(album.getNumOfSongs() + " songs");
/*loading album cover using Glide library*/
Glide.with(mContext).load(album.getThumbnail()).into(holder.thumbnail);
holder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu(holder.overflow, position);
}
});
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onCardSelected(position, holder.thumbnail);
}
});
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onCardSelected(position, holder.thumbnail);
}
});
}
/**
* Showing popup menu when tapping on 3 dots
*/
private void showPopupMenu(View view, int position) {
PopupMenu popup = new PopupMenu(mContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_album, popup.getMenu());
popup.setOnMenuItemClickListener(new MyMenuItemClickListener(position));
popup.show();
}
/**
* Click listener for popup menu items
*/
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
int position;
public MyMenuItemClickListener(int position) {
this.position = position;
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_add_favourite:
listener.onAddToFavoriteSelected(position);
return true;
case R.id.action_play_next:
listener.onPlayNextSelected(position);
return true;
default:
}
return false;
}
}
@Override
public int getItemCount() {
return albumList.size();
}
public interface AlbumsAdapterListener {
void onAddToFavoriteSelected(int position);
void onPlayNextSelected(int position);
void onCardSelected(int position, ImageView thumbnail);
}
}
,错误日志为
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.recyclerviewah.AlbumsAdapter$AlbumsAdapterListener.onCardSelected(int, android.widget.ImageView)' on a null object reference
at com.example.recyclerviewah.AlbumsAdapter$3.onClick(AlbumsAdapter.java:91)
我不明白我需要做什么