Unexpected ClassCastException with findViewById

I play with "search literature" to get into Android development.

My problem is that when I change the layouts of the XML, I get several ClassCastException.

I assume the R file is outdated, but the strange thing is that I still have a problem even after recreating it.

Here is the code snippet and the log:

Log file:

Called: java.lang.ClassCastException: android.widget.ImageButton E / AndroidRuntime (438): when eu.accleaner.android.WordActivity.onCreate (WordActivity.java:87)

The incriminated line in the Office:

mDefinition = (TextView) findViewById(R.id.definition);

Thanks in advance for your help.

Greetings

Vincent

+3
source share
3 answers

, ImageButton XML "definition", TextView. TextView ImageButton.

+3

. R.java android: id 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;

- R.id.definition 0x7f050002 mDefinition = (TextView) findViewById(R.id.definition); (ImageButton02) R.java.

, ClassCastException

+3

Workaround to fix: Assign a new id to the XML layout and findViewById ().

This is most likely a mistake.

0
source

Source: https://habr.com/ru/post/1782623/


All Articles