findViewById不适用于特定视图

Can*_*ğlu 2 android android-layout findviewbyid

我有一个从XML加载的活动,其中的视图具有通常的ID:

<?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>
Run Code Online (Sandbox Code Playgroud)

我试图在代码中引用三个视图:

public ItemViewHolder(View itemView) {
    super(itemView);
    nameView = itemView.findViewById(R.id.nameView);
    priceView = itemView.findViewById(R.id.priceView);
    imageView = itemView.findViewById(R.id.imageView);
}
Run Code Online (Sandbox Code Playgroud)

nameView并且priceView被正确引用,但是imageView没有被引用,并且是null:

在此输入图像描述

为什么我不能参考imageView

(如果我遍历视图树,它就那里.)

更新:我同时执行了Clean Project和Invalidate Caches/Restart,问题仍然存在.

更新2: ItemViewHolder源自RecyclerView.ViewHolder.CircleImageView源自FrameLayout( ImageView).这个XML是我的视图持有者的布局.

更新3:这是我的圈子视图的构造函数:

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

    ...
}
Run Code Online (Sandbox Code Playgroud)

此外,正如Subzero所指出的,我已经检查了mID视图的ID属性(字段),而-1这似乎是导致问题的原因.我不知道为什么它是-1.

Mik*_* M. 5

super将双参数构造函数中的调用更改为:

super(context, attrs);
Run Code Online (Sandbox Code Playgroud)

当a View从布局中膨胀时,XML属性及其值将通过以下方式传递给双参数构造函数AttributeSet.如果您没有将它传递给超类,那么id您在XML中指定的内容永远不会被设置View,因此findViewById()无法使用给定的ID找到它.