java - ClassCastException : getApplication() returns something weird

标签 java android

我有一个带有自定义应用程序类的 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 .它们起源于
  • 华为荣耀 10 (HWCOL),Android 9。
  • 一加一加3T(一加3T),Android 8.0

  • 这怎么可能?

    堆栈是:
    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-class Application 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 the Application object. If you can't avoid those patterns, then consider implementing Key/Value backup or disabling backup entirely.

    developer.android.com via Pablo Baxter

    关于java - ClassCastException : getApplication() returns something weird,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54724612/

    相关文章:

    java - 不使用 Mapper 类读取 XML

    java - 使用正则表达式仅在循环中提取链接

    android - ContactsContract.Contacts 默认是空的吗?

    java - 使用 eclipse 进行 mySql 连接创建基于 Android 的应用程序时遇到问题

    java - 如何在 Android 中保存和加载 GridView 的状态

    java - 如何在Java中对不同数字的字符串进行正确排序?

    java - 如何在 Java 中渲染二维图像

    java - GenericArrayType 接口(interface)的实例

    java - 修改 HTML 和 JSP

    android - MaterialDatePicker获取所选日期