Skip to content Skip to sidebar Skip to footer

Findviewbyid Not Working For Specific View

I have an activity loaded from XML, with views having IDs as usual: Copy

When a View is inflated from a layout, the XML attributes and their values are passed into the two-parameter constructor via the AttributeSet. If you don't pass that to the superclass, the id you've specified in the XML is never set on the View, so findViewById() won't find it with the given ID.

Solution 2:

I like Use this as follows, hope it is helpful.

publicclassCircleImageViewextendsFrameLayout {

    publicCircleImageView(Context context) {
        this(context,null);
    }

    publicCircleImageView(Context context, AttributeSet attrs) {
        this(context.attrs,0);
    }

    publicCircleImageView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    ...
}

Solution 3:

I think the problem is, that your imageView in layout.xml is a custom view (com.myapp.views.CircleImageView), not a regular android ImageView. Try cast to ImageView, if the class extends imageview

ImageViewiv= (ImageView) itemView.findViewById(R.id.imageView));

or use it as your custom view

CircleImageViewiv= (CircleImageView) itemView.findViewById(R.id.imageView));

Post a Comment for "Findviewbyid Not Working For Specific View"