在活动启动时的Andr​​oid抛出ClassCastException抛出、启动时、oid、Andr

2023-09-12 04:27:29 作者:26.半痴半醉半虚伪

我在Android的一个简单的活动计划。 基本上类只是扩展了活动。 但是,当我启动我在我的类的构造一个ClassCastException。 我不甚至有一个构造函数的定义,所以它必须是在活动是父类的构造函数。

不幸的是,调试犯规给它试图投什么类的任何详细信息。

这里是堆栈跟踪:

 发[< 1>主](暂停(RuntimeException的除外))
    ActivityThread $ PackageInfo.makeApplication(布尔值,仪器仪表)线:649
    ActivityThread.handleBindApplication(ActivityThread $ AppBindData)线:4232
    ActivityThread.access $ 3000(ActivityThread,ActivityThread $ AppBindData)线:125
    ActivityThread $ H.handleMessage(消息)线:2071
    ActivityThread $ H(处理器).dispatchMessage(消息)线:99
    Looper.loop()线:123
    ActivityThread.main(字符串[])线:4627
    Method.invokeNative(对象,对象[],上课,下课[],类,整型,布尔)行:不可用[本地方法]
    Method.invoke(对象,对象...)线:521
    ZygoteInit $ MethodAndArgsCaller.run()线:868
    ZygoteInit.main(字符串[])线:626
    NativeStart.main(字符串[])行:不可用[本地方法]
 

当我看着这个RuntimeException的我得到:

detailMessage无法实例化应用com.test.MyApp:java.lang.ClassCastException:com.test.MyApp(ID = 830067694464)

只有code是

  com.test包;
进口android.app.Activity;
公共类MyApp的延伸活动{

}
 

解决方案 开启的Andr​​oidManifest.xml 找到标记应用程序 删除属性安卓名称(如果存在) 添加属性安卓名=android.app.Application

这是我做过什么,问题已经走了。

PS:在步骤4中使用自定义的应用程序类的名称,如果你有一个(这是可选)

i have a simple activity program in android. basically the class just extends Activity. But when i start it i get a ClassCastException in the constructor of my class. i dont even have a constructor defined, so it must be in the constructor of the superclass which is Activity.

unfortunately the debugger doesnt give any detailed information on what class it is trying to cast.

here is the stacktrace:

Thread [<1> main] (Suspended (exception RuntimeException))  
    ActivityThread$PackageInfo.makeApplication(boolean, Instrumentation) line: 649  
    ActivityThread.handleBindApplication(ActivityThread$AppBindData) line: 4232 
    ActivityThread.access$3000(ActivityThread, ActivityThread$AppBindData) line: 125    
    ActivityThread$H.handleMessage(Message) line: 2071  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4627    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 868  
    ZygoteInit.main(String[]) line: 626 
    NativeStart.main(String[]) line: not available [native method]  

and when i look into this runtimeexception i get:

detailMessage "Unable to instantiate application com.test.MyApp: java.lang.ClassCastException: com.test.MyApp" (id=830067694464)

the only code is

package com.test;
import android.app.Activity;
public class MyApp extends Activity {

}

解决方案

Open AndroidManifest.xml Find tag application Remove attribute android:name (if exists) Add attribute android:name="android.app.Application"

This is what I did and the problem had gone.

P.S: In Step 4 use your custom application class name if you have one (that's optional).