findViewById在ImageView控件上为我返回null。 logcat中没有错误,没有任何内容表明发生了什么。 id的匹配和其他图像视图正确设置。 Java和xml通过xml中的类标记进行链接,指向java中定义的类,该类是RelativeLayout的后代。为什么findViewById(...)返回null?
我试着改变R.id.more_icon1的名称,但没有奏效。试过清洁,并没有奏效。使用调试器来查看它实际上是否继续前进,何时返回mMoreIcon == null。
奇怪的是其他ImageView的工作很好。
以前有人看过这个或有什么想法吗?
Java代码:类是RelativeLayout的
的后裔@Override
protected void onFinishInflate() {
super.onFinishInflate();
mText1 = (TextView) findViewById(R.id.text1);
mText2 = (TextView) findViewById(R.id.text2);
mIcon1 = (ImageView) findViewById(R.id.icon1);
mIcon2 = (ImageView) findViewById(R.id.icon2);
// mMoreIcon is the one that gets set as null. mIcon1 and mIcon2 work just fine.
mMoreIcon = (ImageView) findViewById(R.id.more_icon1);
}
XML代码:
<ImageView android:id="@+id/icon1"
style="@style/SuggestionIcon1"
android:scaleType="centerInside"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
/>
<!-- This is the icon that is being returned as null -->
<ImageView android:id="@+id/more_icon1"
style="@style/MoreIcon2"
android:scaleType="centerInside"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:visibility="gone" />
<ImageView android:id="@+id/icon2"
style="@style/SuggestionIcon2"
android:scaleType="centerInside"
android:layout_toLeftOf="@id/more_icon1"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:visibility="gone" />
感谢
从我这边来的愚蠢的问题,但你有没有尝试重新创建R.java文件(从Eclipse中删除它,它将被重新创建)或使用不同的名称为按钮?当我们在身份证中使用身份证号时,我曾经遇到过同样的问题。 –
不是一个愚蠢的问题,但是我有。我做了一个清理,它删除R.java文件并重新创建所有生成的文件。我也尝试更改名称。起初它只是更多图标。 – FuegoFingers
嗯。现在这是一个奇怪的。我试图重新创建你的布局(除了布局是RelativeLayout的后代,但这不应该是一个问题),它对我来说工作得很好。 我真的不知道这个问题可能会发生什么。总是试图擦除/ bin文件夹并重建项目。这为我解决了以前几个无法解释的问题。 –