findViewById不适用于特定视图 [英] findViewById not working for specific view
问题描述
我有一个从XML加载的活动,其视图具有照常的ID:
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
和 priceView
,但是未引用 imageView
,并且为 null
:
nameView
and priceView
are referenced correctly, however, imageView
isn't being referenced and is null
:
为什么我不能引用 imageView
?
(如果我遍历视图树,它在这里 .)
(If I traverse the view tree, it is there.)
更新:我同时执行了Clean Project和Invalidate Caches/Restart,问题仍然存在.
UPDATE: I did both Clean Project and Invalidate Caches/Restart, the problem persists.
更新2: ItemViewHolder
是从 RecyclerView.ViewHolder
派生的. CircleImageView
源自 FrameLayout
(不是 ImageView
).该XML是我的视图持有者的布局.
UPDATE 2: ItemViewHolder
is derived from RecyclerView.ViewHolder
. CircleImageView
is derived from FrameLayout
(not ImageView
). This XML is the layout of my view holder.
更新3:这是我的圈子视图的构造函数:
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();
}
...
}
此外,正如Subzero指出的那样,我已经检查了视图的ID属性( mID
字段),它是 -1
,这似乎是导致问题.我不知道为什么它是-1.
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.
推荐答案
将两参数构造函数中的 super
调用更改为:
Change the super
call in your two-parameter constructor to:
super(context, attrs);
当从布局中扩展 View
时,XML属性及其值将通过 AttributeSet
传递到两参数构造函数中.如果不将其传递给超类,则在XML中指定的 id
永远不会在 View
上设置,因此 findViewById()
找不到具有给定ID的文件.
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.
这篇关于findViewById不适用于特定视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!