Java findviewbyid无法解决android studio中的方法错误
我是android开发的新手。。。为什么会出现此错误:-“findviewbyid无法解析方法”,而在viewholder方法中使用findviewbyid。。。提前谢谢! 我想从我的绘图中添加图像,为此我使用recyclerview。。。代码如下:-Java findviewbyid无法解决android studio中的方法错误,java,android,Java,Android,我是android开发的新手。。。为什么会出现此错误:-“findviewbyid无法解析方法”,而在viewholder方法中使用findviewbyid。。。提前谢谢! 我想从我的绘图中添加图像,为此我使用recyclerview。。。代码如下:- public class ViewHolder extends RecyclerView.ViewHolder{ TextView stateName; ImageView stateImage; public View
public class ViewHolder extends RecyclerView.ViewHolder{
TextView stateName;
ImageView stateImage;
public ViewHolder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(v.getContext(),Details.class);
i.putExtra("titleOfState",stitles[getAdapterPosition()]);
i.putExtra("contentOfState",scontents[getAdapterPosition()]);
v.getContext().startActivity(i);
}
});
stateName=itemView.findViewById(R.id.stateName);
stateImage=(ImageView)findViewById(R.id.imageView);
}
}
findViewById()
是一种查看对象的方法,而不是查看持有者的方法
只需在itemView
findViewById()
上调用它,它是查看对象的方法,而不是查看持有者的方法
只需在
itemView
上调用它,您刚刚错过了对itemView
的引用,用这一行替换stateImage行,它应该可以正常工作
stateImage = itemView.findViewById(R.id.imageView);
您刚刚错过了对
itemView
的引用,请用这一行替换stateImage行,它应该可以正常工作
stateImage = itemView.findViewById(R.id.imageView);