[英]findviewbyid cannot resolve method error in android studio
Im new in android development... why im getting this error:- "findviewbyid cannot resolve method" while using findviewbyid in viewholder method... Thanks in advance !我是 android 开发的新手...为什么我会收到此错误:-在 viewholder 方法中使用 findviewbyid 时“findviewbyid 无法解析方法”...提前致谢! I want to add image from my drawable, im using recyclerview for this purpose... here's the code:-
我想从我的可绘制对象中添加图像,为此我使用了 recyclerview ......这是代码:-
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()
is a method of View
object and not ViewHolder
. findViewById()
是View
对象的方法,而不是ViewHolder
。
Just call it on itemView
只需在
itemView
上调用它
你刚好错过指的itemView
,这条线替换你的stateImage线,它应该工作的罚款。
stateImage = itemView.findViewById(R.id.imageView);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.