在开发人员控制台中,我看到很多这样的堆栈跟踪崩溃
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 方法是
public App getApp() {
return (App) getApplication();
}
应用类是
public class App extends MultiDexApplication { ...
并且在清单应用程序标记中包含对此类的引用
<application
android:name="com.myapp.App"
98% 的崩溃是针对 android 7.0,其余的是 7.1。其他安卓版本不受影响。
编辑:我使用 proguard 所以它可以以某种方式相关但保持类
-keep class com.myapp.** { *;}
-keep interface com.myapp.** { *;}
注意:它可能不相关,但在相同的 android 版本中,看起来 App 的 onCreate 方法有时没有被调用。我观察到它是因为在 onCreate 中创建的某些对象在从 Service(由 AlarmManager 启动)或 BroadcastReceiver 访问时为空
有谁知道什么会导致它,如何修复它或解决它?谢谢
编辑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;
}
它至少不会崩溃,我可以检查 getApp() == null