Android 应用程序打开时崩溃

发布于 2025-01-08 10:56:54 字数 2725 浏览 0 评论 0 原文

我正在开发一个 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

I'm developing an android application and as soon as the AVD opens and I unlock the device and click the menu, I get an "Application stops unexpectedly" error. The following is my logcat log.

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

EDIT: code available here: http://pastebin.com/VSvWvgGa

EDIT2: XML available here: http://pastebin.com/uGLpyUcW

EDIT3: After making the initial changes Druv recommended, I'm getting new logcat errors and I've updated my post with those errors

EDIT4: R.layout.main.xml: http://pastebin.com/Qe8Bx47m

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔戏命师 2025-01-15 10:56:54
    AndroidManifest.xml 
    <application android:name="ComplimentGenerator" android:icon="@drawable/icon">

 Just Rename Your
      android:name="ComplimentGenerator"  to   android:label="ComplimentGenerator"
    AndroidManifest.xml 
    <application android:name="ComplimentGenerator" android:icon="@drawable/icon">

 Just Rename Your
      android:name="ComplimentGenerator"  to   android:label="ComplimentGenerator"
对你再特殊 2025-01-15 10:56:54

我认为“android:name”属性值是错误的!使用以下内容(以“.”开头):

<activity android:name=".ComplimentGenerator" android:label="ComplimentGenerator">

这样,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 "."):

<activity android:name=".ComplimentGenerator" android:label="ComplimentGenerator">

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

人│生佛魔见 2025-01-15 10:56:54

您可以在此处添加 com.compliment:

<activity android:name="com.compliment.ComplimentGenerator" android:label="ComplimentGenerator">

编辑:
更改为 文档,

公开课申请

需要维护全局应用程序状态的基类。
您可以通过在您的文件中指定其名称来提供您自己的实现
AndroidManifest.xml 的标签,这将导致
当您的流程为您实例化时

应用程序/包已创建。

通常不需要子类化应用程序。在大多数情况下,
静态单例可以以更加模块化的方式提供相同的功能
方式。如果您的单例需要全局上下文(例如注册
广播接收器),检索它的函数可以给出
内部使用 Context.getApplicationContext() 的上下文
首先构建单例。

换句话说,当您添加 android:name 时,ComplimentGenerator 类就会被实例化。但是,ComplimentGenerator 并未对 Application 进行子类化(这是必需的),这导致了 ClassCastException。

Could you add com.compliment over here:

<activity android:name="com.compliment.ComplimentGenerator" android:label="ComplimentGenerator">

Edit:
Change <activity android:name="ComplimentGenerator" android:label="ComplimentGenerator"> to <activity android:icon="@drawable/icon" android:label="ComplimentGenerator">
From the docs,

public class Application

Base class for those who need to maintain global application state.
You can provide your own implementation by specifying its name in your
AndroidManifest.xml's tag, which will cause that class
to be instantiated for you
when the process for your
application/package is created.

There is normally no need to subclass Application. In most situation,
static singletons can provide the same functionality in a more modular
way. If your singleton needs a global context (for example to register
broadcast receivers), the function to retrieve it can be given a
Context which internally uses Context.getApplicationContext() when
first constructing the singleton.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文