我在Android中创建多个activities
,这是我的logcat错误输出。
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
这是我的java文件Converter.java
http://pastebin.com/VNNPy7D5
谢谢你们:)
答案 0 :(得分:2)
如果我算上这个
ibHome3 = (Button) findViewById(R.id.ibHome3);
导致您的问题。你在xml中将它作为ImageButton
但在java声明中作为Button
。只需将其更改为
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)
这告诉我们问题从第41行的Converter.java
开始
答案 1 :(得分:1)
布局文件中有两个按钮,对吗?它们看起来在XML文件中被定义为ImageButton。在您的Activity中,您将其声明为Button并尝试使用强制转换转换为(Button)。在Activity文件中将类型更改为ImageButton。它可以解决你的问题。
答案 2 :(得分:0)
在第41行,您必须将按钮投射到ImageButton而不是按钮:
ibHome3 = (ImageButton) findViewById(R.id.ibHome3);