0

I am creating multiple activities in Android, this is my logcat error output.

log.txt

05-03 03:17:23.295: E/PhonePolicy(1854): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
05-03 03:17:37.044: W/dalvikvm(1854): threadid=1: thread exiting with uncaught exception (group=0x409db1f8)
05-03 03:17:37.044: E/AndroidRuntime(1854): FATAL EXCEPTION: main
05-03 03:17:37.044: E/AndroidRuntime(1854): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jumoun.itemp/com.jumoun.itemp.Converter}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-03 03:17:37.044: E/AndroidRuntime(1854):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at android.os.Looper.loop(Looper.java:137)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at android.app.ActivityThread.main(ActivityThread.java:4427)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at java.lang.reflect.Method.invokeNative(Native Method)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at java.lang.reflect.Method.invoke(Method.java:511)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at dalvik.system.NativeStart.main(Native Method)
05-03 03:17:37.044: E/AndroidRuntime(1854): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-03 03:17:37.044: E/AndroidRuntime(1854):     at com.jumoun.itemp.Converter.onCreate(Converter.java:41)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at android.app.Activity.performCreate(Activity.java:4465)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-03 03:17:37.044: E/AndroidRuntime(1854):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-03 03:17:37.044: E/AndroidRuntime(1854):     ... 11 more
05-03 03:17:38.704: I/Process(1854): Sending signal. PID: 1854 SIG: 9

This is my java file Converter.java http://pastebin.com/VNNPy7D5

Thanks guys :)

4

3 回答 3

2

如果我数对了

ibHome3 = (Button) findViewById(R.id.ibHome3);

导致你的问题。您将它作为一个ImageButton在您的 xml 中但Button在您的 java 声明中。只需将其更改为

ImageButton ibHome3;

关于 Logcat

另外,只是对 logcat 的一点建议。如果你发现第一行说Caused By之后Fatal Exception你可以更容易地找到你的问题。这里是

 Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button

这给出了例外

java.lang.ClassCastException

然后找到引用您的包的第一行。这里

at com.jumoun.itemp.Converter.onCreate(Converter.java:41)

这告诉我们问题从Converter.java第 41 行开始

于 2013-05-02T19:26:29.787 回答
1

你的布局文件中有两个按钮,对吧?它们看起来在 XML 文件中被定义为 ImageButton。在您的 Activity 中,您将其声明为 Button 并尝试使用强制转换转换为 (Button)。在 Activity 文件中将类型更改为 ImageButton。它可能会解决您的问题。

于 2013-05-02T19:26:35.043 回答
0

在第 41 行,您必须将按钮转换为 ImageButton 而不是 Button :

ibHome3 = (ImageButton) findViewById(R.id.ibHome3);
于 2013-05-02T19:25:16.140 回答