findViewById not working for specific view

蹲街弑〆低调 提交于 2021-02-05 06:38:21

问题


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

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="80dp"
    android:layout_height="110dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/white_circle">

            <com.myapp.views.CircleImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="16dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="-12dp"
            android:background="@drawable/price_background">

            <TextView
                android:id="@+id/priceView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:text="0.1 BTC"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textSize="10sp"
                android:textStyle="bold" />

        </FrameLayout>

        <TextView
            android:id="@+id/nameView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:gravity="center_horizontal"
            android:lineSpacingMultiplier="0.8"
            android:lines="2"
            android:text="Bacon Cheeseburger"
            android:textSize="10sp" />
    </LinearLayout>
</FrameLayout>

I'm trying to reference three views in code:

public ItemViewHolder(View itemView) {
    super(itemView);
    nameView = itemView.findViewById(R.id.nameView);
    priceView = itemView.findViewById(R.id.priceView);
    imageView = itemView.findViewById(R.id.imageView);
}

nameView and priceView are referenced correctly, however, imageView isn't being referenced and is null:

Why can't I reference imageView?

(If I traverse the view tree, it is there.)

UPDATE: I did both Clean Project and Invalidate Caches/Restart, the problem persists.

UPDATE 2: ItemViewHolder is derived from RecyclerView.ViewHolder. CircleImageView is derived from FrameLayout (not ImageView). This XML is the layout of my view holder.

UPDATE 3: Here is my circle view's constructor:

public class CircleImageView extends FrameLayout {

    public CircleImageView(Context context) {
        super(context);
        init();
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context);
        init();
    }

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

    ...
}

Also, as Subzero noted, I've checked the ID property (mID field) of the view, and it's -1, which seems to be the cause of the problem. I have no idea why it is -1 though.


回答1:


Change the super call in your two-parameter constructor to:

super(context, attrs);

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.




回答2:


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

public class CircleImageView extends FrameLayout {

    public CircleImageView(Context context) {
        this(context,null);
    }

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

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



回答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

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

or use it as your custom view

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


来源:https://stackoverflow.com/questions/51418503/findviewbyid-not-working-for-specific-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!