Android 应用程序打开时崩溃
我正在开发一个 Android 应用程序,一旦 AVD 打开并解锁设备并单击菜单,我就会收到“应用程序意外停止”错误。以下是我的logcat日志。
02-22 11:34:38.220: D/AndroidRuntime(336): Shutting down VM
02-22 11:34:38.220: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-22 11:34:38.290: E/AndroidRuntime(336): FATAL EXCEPTION: main
02-22 11:34:38.290: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to instantiate application com.compliment.ComplimentGenerator: java.lang.ClassCastException: com.compliment.ComplimentGenerator
02-22 11:34:38.290: E/AndroidRuntime(336): at android.app.LoadedApk.makeApplication(LoadedApk.java:466)
02-22 11:34:38.290: E/AndroidRuntime(336): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3260)
02-22 11:34:38.290: E/AndroidRuntime(336): at android.app.ActivityThread.access$2200(ActivityThread.java:117)
02-22 11:34:38.290: E/AndroidRuntime(336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
02-22 11:34:38.290: E/AndroidRuntime(336): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 11:34:38.290: E/AndroidRuntime(336): at android.os.Looper.loop(Looper.java:123)
02-22 11:34:38.290: E/AndroidRuntime(336): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-22 11:34:38.290: E/AndroidRuntime(336): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 11:34:38.290: E/AndroidRuntime(336): at java.lang.reflect.Method.invoke(Method.java:507)
02-22 11:34:38.290: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-22 11:34:38.290: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-22 11:34:38.290: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method)
02-22 11:34:38.290: E/AndroidRuntime(336): Caused by: java.lang.ClassCastException: com.compliment.ComplimentGenerator
02-22 11:34:38.290: E/AndroidRuntime(336): at android.app.Instrumentation.newApplication(Instrumentation.java:957)
02-22 11:34:38.290: E/AndroidRuntime(336): at android.app.Instrumentation.newApplication(Instrumentation.java:942)
02-22 11:34:38.290: E/AndroidRuntime(336): at android.app.LoadedApk.makeApplication(LoadedApk.java:461)
02-22 11:34:38.290: E/AndroidRuntime(336): ... 11 more
编辑:此处提供代码:http://pastebin.com/VSvWvgGa
编辑2:此处提供XML:http://pastebin.com/uGLpyUcW
EDIT3:进行初始更改 Druv 后推荐,我收到新的 logcat 错误,并且我已经用这些错误更新了我的帖子
EDIT4: R.layout.main.xml: http://pastebin.com/Qe8Bx47m
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为“android:name”属性值是错误的!使用以下内容(以“.”开头):
这样,Android 就会知道启动的 Activity 是 com.compliment.ComplimentGenerator
"com.compliment" 来自于 "package" 属性值
".ComplimentGenerator" 来自于 "android:name" 属性值
I think the "android:name" attribute value is wrong! Use the following (starting with the "."):
Thus, Android will know the starting activity is com.compliment.ComplimentGenerator
"com.compliment" comes from the "package" attribute value
".ComplimentGenerator" comes from the "android:name" attribute value
您可以在此处添加 com.compliment:
编辑:文档,
将
更改为换句话说,当您添加 android:name 时,ComplimentGenerator 类就会被实例化。但是,ComplimentGenerator 并未对 Application 进行子类化(这是必需的),这导致了 ClassCastException。
Could you add com.compliment over here:
Edit:
Change
<activity android:name="ComplimentGenerator" android:label="ComplimentGenerator">
to<activity android:icon="@drawable/icon" android:label="ComplimentGenerator">
From the docs,
In other words, when you added android:name, the ComplimentGenerator class was being instantiated. However, ComplimentGenerator was not subclassing Application (which was required), which resulted in a ClassCastException.