findViewById不适用于特定视图

Can Poyrazoglu

我有一个从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>

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

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并且priceView被正确引用,但是imageView未被引用,为null

在此处输入图片说明

我为什么不能参考imageView

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

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

更新2: ItemViewHolder源自RecyclerView.ViewHolderCircleImageView源自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();
    }

    ...
}

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

迈克·M

super您的两参数构造函数中调用更改为:

super(context, attrs);

View从布局充气时,XML属性和它们的值被传递到经由所述两参数的构造AttributeSet如果您不将其传递给超类,那么id您在XML中指定的对象就永远不会在上设置View,因此findViewById()不会找到具有给定ID的对象。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章