Erro no comando findViewById

2 respostas
F

Pessoal,

Estou fazendo um exemplo simples e como sou iniciante não consigo entender o erro:

O erro ocorre quando executa a seguinte linha:
exemplo4.java final TextView textResultado = (EditText) findViewById(R.id.campoResultado);Tela:

Emulador: The application LivroAndroidCap3 (process br.livro.android.cap3) has stopped unexpectedly. Please try again.

Log cat:

05-27 12:22:16.938: ERROR/AndroidRuntime(1086): FATAL EXCEPTION: main

05-27 12:22:16.938: ERROR/AndroidRuntime(1086): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.livro.android.cap3/br.livro.android.cap3.Exemplo4}: java.lang.ClassCastException: android.widget.TextView

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at android.os.Handler.dispatchMessage(Handler.java:99)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at android.os.Looper.loop(Looper.java:123)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at android.app.ActivityThread.main(ActivityThread.java:4627)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at java.lang.reflect.Method.invokeNative(Native Method)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at java.lang.reflect.Method.invoke(Method.java:521)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at dalvik.system.NativeStart.main(Native Method)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086): Caused by: java.lang.ClassCastException: android.widget.TextView

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at br.livro.android.cap3.Exemplo4.onCreate(Exemplo4.java:19)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

05-27 12:22:16.938: ERROR/AndroidRuntime(1086):      11 more

05-27 12:34:45.698: ERROR/AndroidRuntime(1116): FATAL EXCEPTION: main

05-27 12:34:45.698: ERROR/AndroidRuntime(1116): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.livro.android.cap3/br.livro.android.cap3.Exemplo4}: java.lang.ClassCastException: android.widget.TextView

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at android.os.Handler.dispatchMessage(Handler.java:99)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at android.os.Looper.loop(Looper.java:123)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at android.app.ActivityThread.main(ActivityThread.java:4627)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at java.lang.reflect.Method.invokeNative(Native Method)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at java.lang.reflect.Method.invoke(Method.java:521)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at dalvik.system.NativeStart.main(Native Method)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116): Caused by: java.lang.ClassCastException: android.widget.TextView

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at br.livro.android.cap3.Exemplo4.onCreate(Exemplo4.java:19)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

05-27 12:34:45.698: ERROR/AndroidRuntime(1116):      11 more

05-27 12:57:41.489: ERROR/AndroidRuntime(1145): FATAL EXCEPTION: main

05-27 12:57:41.489: ERROR/AndroidRuntime(1145): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.livro.android.cap3/br.livro.android.cap3.Exemplo4}: java.lang.ClassCastException: android.widget.TextView

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at android.os.Handler.dispatchMessage(Handler.java:99)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at android.os.Looper.loop(Looper.java:123)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at android.app.ActivityThread.main(ActivityThread.java:4627)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at java.lang.reflect.Method.invokeNative(Native Method)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at java.lang.reflect.Method.invoke(Method.java:521)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at dalvik.system.NativeStart.main(Native Method)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145): Caused by: java.lang.ClassCastException: android.widget.TextView

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at br.livro.android.cap3.Exemplo4.onCreate(Exemplo4.java:19)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

05-27 12:57:41.489: ERROR/AndroidRuntime(1145):      11 more

Agradeço qualquer ajuda.

2 Respostas

leo.alvesneuwald

De:

TextView textResultado = (EditText) findViewById(R.id.campoResultado);

Para:

TextView textResultado = (TextView) findViewById(R.id.campoResultado);

F

Deu certo.

Obrigado leo.alvesneuwald,

Criado 27 de maio de 2012
Ultima resposta 28 de mai. de 2012
Respostas 2
Participantes 2