rad10wave
rad10wave

Reputation: 33

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 ! I want to add image from my drawable, im using recyclerview for this purpose... here's the code:-

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

Snap

Upvotes: 0

Views: 314

Answers (2)

devMohaned
devMohaned

Reputation: 145

You've just missed refering to itemView, replace your stateImage line with this line, and it should work fine.

stateImage = itemView.findViewById(R.id.imageView);

Upvotes: 0

Bruno
Bruno

Reputation: 4007

findViewById() is a method of View object and not ViewHolder.

Just call it on itemView

Upvotes: 2

Related Questions