我有一个带有自定义应用程序类的 Android 应用程序:
<application android:name=".App"
android:icon="@drawable/icon"
android:description="@string/IDS_APPDESC"
android:label="@string/IDS_APPTITLE"
android:theme="@style/Theme.App">
一般工作。在主要 Activity 的
onCreate
,我检索应用程序对象:public App App()
{
return (App)getApplication();
}
现在,我在 Google Play 控制台中有几份崩溃报告说这条线导致了
ClassCastException
.它们起源于这怎么可能?
堆栈是:
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3300)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3484)
at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2123)
at android.os.Handler.dispatchMessage (Handler.java:109)
at android.os.Looper.loop (Looper.java:207)
at android.app.ActivityThread.main (ActivityThread.java:7470)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:958)
Caused by: java.lang.ClassCastException:
at ru.acme.Main.App (Main.java:322)
at ru.acme.Main.onCreate (Main.java:145)
at android.app.Activity.performCreate (Activity.java:7436)
at android.app.Activity.performCreate (Activity.java:7426)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1286)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3279)
EDIT2:有一个名为 App() 的方法返回应用程序,还有一个类 App。 Java 编译器不会提示...
EDIT3:从另一个设备/操作系统得到同样的崩溃。可能不是黑客。
最佳答案
During auto backup and restore operations, the system launches the app in a restricted mode to both prevent the app from accessing files that could cause conflicts and let the app execute callback methods in its
BackupAgent
. In this restricted mode, the app's main activity is not automatically launched, its Content Providers are not initialized, and the base-classApplication
is instantiated instead of any subclass declared in the app's manifest.Caution: To avoid errors, make sure that the parts of your app that execute in the restricted mode (mostly your
BackupAgent
) don't access content providers in the same app or attempt to cast theApplication
object. If you can't avoid those patterns, then consider implementing Key/Value backup or disabling backup entirely.
关于java - ClassCastException : getApplication() returns something weird,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54724612/