Android 7.0和7.1 getApplication()ClassCastException

时间:2022-07-27 15:32:27

In Developer Console I see a lot of crashes with stacktrace like this

在Developer Console中,我看到很多像这样的堆栈跟踪崩溃

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
  at android.app.ActivityThread.-wrap14(ActivityThread.java:0)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6776)
  at java.lang.reflect.Method.invoke(Native Method:0)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.ClassCastException: 
  at com.myapp.ui.BaseActivity.getApp(BaseActivity.java:193)
  at com.myapp.ui.BaseActivity.onCreate(BaseActivity.java:275)
  at com.myapp.ui.CastActivity.onCreate(CastActivity.java:39)
  at com.myapp.ui.MainActivity.onCreate(MainActivity.java:268)
  at android.app.Activity.performCreate(Activity.java:6955)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)

getApp method of BaseActivity is

BaseActivity的getApp方法是

public App getApp() {
        return (App) getApplication();
    }

App class is

App类是

public class App extends MultiDexApplication { ...

and in manifest application tag contains reference to this class

并且在manifest应用程序标记中包含对此类的引用

 <application
        android:name="com.myapp.App"

98% of crashes is for android 7.0, rest is 7.1. No other android versions are affected.

98%的崩溃是针对Android 7.0,休息是7.1。没有其他Android版本受到影响。

EDIT: I use proguard so it can be somehow related but keeping class

编辑:我使用proguard所以它可以以某种方式相关但保持上课

-keep class com.myapp.** { *;}
-keep interface com.myapp.** { *;}

Note: It may not be related but in same android versions it looks like App's onCreate method is sometimes not called. I observed it because some objects which are created in onCreate were null when they were accessed from Service (started by AlarmManager) or BroadcastReceiver

注意:它可能没有关系,但在相同的Android版本中,它看起来像App的onCreate方法有时不被调用。我观察到它是因为在onCreate中创建的某些对象在从Service(由AlarmManager启动)或BroadcastReceiver访问时为null

Does anyone has idea what can cause it, how to fix it or work around it? Thanks

有谁知道什么可以导致它,如何解决它或解决它?谢谢

EDIT 2: I ended up with something like this:

编辑2:我最终得到了这样的东西:

   public App getApp() {

    Application application = getApplication();
    App app = null;

    try {
        app = (App) application;
    } catch (Exception e) {
        if (application != null) {
            Log.e(TAG, "getApp Exception: application class: " + application.getClass().getName());
        } else {
            Log.e(TAG, "getApp Exception: application object is null");
        }
    }

    return app;
}

It at least doesn't crash and I can check getApp() == null

它至少不会崩溃,我可以检查getApp()== null

5 个解决方案

#1


9  

Casting fails because getApplication() returns an Application and NOT the desired sub-class.

转换失败,因为getApplication()返回一个Application而不是所需的子类。

I've had some success where I caught the error and asked the user to reboot their device or reinstall the app.

我已经取得了一些成功,我发现错误并要求用户重新启动他们的设备或重新安装应用程序。

Unfortunately, there's no real fix to this rare crash. Google won't fix the lifecycle-related issue, but said it reduced in Android 7.1+. Source: https://issuetracker.google.com/issues/37137009

不幸的是,这种罕见的崩溃没有真正的解决办法。谷歌不会修复与生命周期相关的问题,但表示它在Android 7.1+中有所减少。资料来源:https://issuetracker.google.com/issues/37137009

#2


2  

I think you should cast getApplicationContext() into App instead.

我认为你应该将getApplicationContext()转换为App。

#3


0  

You should override attachBaseContext in your application class like this:

您应该在应用程序类中覆盖attachBaseContext,如下所示:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

Check this link for more information: https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html

有关详细信息,请查看此链接:https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html

#4


0  

This might help

这可能有所帮助

public class App extends MultiDexApplication { 

        public static App app = null;

        public static App getInstance() {
            return app;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            app = this;
        }
}

you doesn't need to cast getApplication(), reason is you are already in Application class so simply just use this keyword to get application instance. Hope you find useful

你不需要强制转换getApplication(),原因是你已经在Application类中,所以只需使用这个关键字来获取应用程序实例。希望你找到有用的

#5


0  

While I cannot say if this solution works.

虽然我不能说这个解决方案是否有效。

I think that static Application instance should solve the problem.

我认为静态Application实例应该解决问题。

class MyApp extends Application {
  private static final sInstance;

  public void onCreate() {
    sInstance = this;
  }

  public static MyApp getInstance() {
    return sInstance;
  }
}

Instead of calling getActivity() if you call MyApp.getInstance() you should not need to cast. So there should not be any ClassCastException anymore.

如果调用MyApp.getInstance()而不是调用getActivity(),则不需要强制转换。所以不应再有任何ClassCastException了。

#1


9  

Casting fails because getApplication() returns an Application and NOT the desired sub-class.

转换失败,因为getApplication()返回一个Application而不是所需的子类。

I've had some success where I caught the error and asked the user to reboot their device or reinstall the app.

我已经取得了一些成功,我发现错误并要求用户重新启动他们的设备或重新安装应用程序。

Unfortunately, there's no real fix to this rare crash. Google won't fix the lifecycle-related issue, but said it reduced in Android 7.1+. Source: https://issuetracker.google.com/issues/37137009

不幸的是,这种罕见的崩溃没有真正的解决办法。谷歌不会修复与生命周期相关的问题,但表示它在Android 7.1+中有所减少。资料来源:https://issuetracker.google.com/issues/37137009

#2


2  

I think you should cast getApplicationContext() into App instead.

我认为你应该将getApplicationContext()转换为App。

#3


0  

You should override attachBaseContext in your application class like this:

您应该在应用程序类中覆盖attachBaseContext,如下所示:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

Check this link for more information: https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html

有关详细信息,请查看此链接:https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html

#4


0  

This might help

这可能有所帮助

public class App extends MultiDexApplication { 

        public static App app = null;

        public static App getInstance() {
            return app;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            app = this;
        }
}

you doesn't need to cast getApplication(), reason is you are already in Application class so simply just use this keyword to get application instance. Hope you find useful

你不需要强制转换getApplication(),原因是你已经在Application类中,所以只需使用这个关键字来获取应用程序实例。希望你找到有用的

#5


0  

While I cannot say if this solution works.

虽然我不能说这个解决方案是否有效。

I think that static Application instance should solve the problem.

我认为静态Application实例应该解决问题。

class MyApp extends Application {
  private static final sInstance;

  public void onCreate() {
    sInstance = this;
  }

  public static MyApp getInstance() {
    return sInstance;
  }
}

Instead of calling getActivity() if you call MyApp.getInstance() you should not need to cast. So there should not be any ClassCastException anymore.

如果调用MyApp.getInstance()而不是调用getActivity(),则不需要强制转换。所以不应再有任何ClassCastException了。