意外的ClassCastException与findViewById [英] Unexpected ClassCastException with findViewById
问题描述
我玩弄可搜索词典"进入Android开发.
I play around with the 'searchable dictionnary' to get into Android development.
我的问题是修改XML布局时得到了ClassCastException
.
My problem is that I get some ClassCastException
when modifying the XML layouts.
我的猜测是R文件已经过时,但是奇怪的是,即使重新创建后我仍然有问题.
My guess is that the R file is outdated, but what is weird is that I still have the problem even after recreating it.
这是相关的代码和日志:
Here are the releveant piece of code and log :
日志文件:
原因:java.lang.ClassCastException:android.widget.ImageButton E/AndroidRuntime(438):在 eu.accleaner.android.WordActivity.onCreate(WordActivity.java:87)
Caused by: java.lang.ClassCastException: android.widget.ImageButton E/AndroidRuntime( 438): at eu.accleaner.android.WordActivity.onCreate(WordActivity.java:87)
活动"中的斜线:
mDefinition = (TextView) findViewById(R.id.definition);
预先感谢您的帮助.
干杯
Vincent
推荐答案
我遇到了类似的问题. R.java基于xml中的android:id生成ID:
public static final int imageButton01=0x7f050001;
public static final int definition=0x7f050002;
I had similar issue. R.java generates IDs based on android:id in xml:
public static final int imageButton01=0x7f050001;
public static final int definition=0x7f050002;
当我添加新的图像按钮时,R.java将更新为
public static final int imageButton01=0x7f050001;
public static final int imageButton02=0x7f050002;
public static final int definition=0x7f050003;
When I add new imagebutton R.java will update to
public static final int imageButton01=0x7f050001;
public static final int imageButton02=0x7f050002;
public static final int definition=0x7f050003;
由于同步问题,R.id.definition
返回了mDefinition = (TextView) findViewById(R.id.definition);
中的旧ID 0x7f050002
,但根据更新的R.java,它对应于另一个元素(ImageButton02).
Due to synchronization problem R.id.definition
returns old ID 0x7f050002
in mDefinition = (TextView) findViewById(R.id.definition);
But it corresponds to another element (ImageButton02) according to updated R.java.
所以我们有ClassCastException
So we have ClassCastException
这篇关于意外的ClassCastException与findViewById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!