Android 7.0和7.1 getApplication()ClassCastException [英] Android 7.0 and 7.1 getApplication() ClassCastException
问题描述
在开发人员控制台中,我看到很多这样的stacktrace崩溃
In Developer Console I see a lot of crashes with stacktrace like this
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)
BaseActivity的getApp方法是
getApp method of BaseActivity is
public App getApp() {
return (App) getApplication();
}
App类为
public class App extends MultiDexApplication { ...
并且清单应用程序标记中包含对此类的引用
and in manifest application tag contains reference to this class
<application
android:name="com.myapp.App"
98%的崩溃是针对android 7.0,其余为7.1.没有其他Android版本受到影响.
98% of crashes is for android 7.0, rest is 7.1. No other android versions are affected.
我使用proguard,因此可以某种方式关联但可以保持班级
I use proguard so it can be somehow related but keeping class
-keep class com.myapp.** { *;}
-keep interface com.myapp.** { *;}
注意:它可能不相关,但是在相同的android版本中,有时似乎未调用App的onCreate方法.我观察到它是因为从服务(由AlarmManager启动)或BroadcastReceiver对其进行访问时,在onCreate中创建的某些对象为空
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
有人知道会导致什么,如何修复或解决它吗?谢谢
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:
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;
}
至少不会崩溃,我可以检查getApp()== null
It at least doesn't crash and I can check getApp() == null
推荐答案
铸造失败,因为getApplication()
返回的是Application
而不是所需的子类.
Casting fails because getApplication()
returns an Application
and NOT the desired sub-class.
我在捕获错误并要求用户重新启动设备或重新安装应用程序方面取得了一些成功.
I've had some success where I caught the error and asked the user to reboot their device or reinstall the app.
不幸的是,对于这种罕见的崩溃并没有真正的解决方法. Google不会解决与生命周期相关的问题,但表示已在Android 7.1+中减少了该问题.来源: https://issuetracker.google.com/issues/37137009
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.0和7.1 getApplication()ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!