java.lang.ClassCastException:android.app.Application无法强制转换为com.xxx.xxx.MyApplication

时间:2016-10-27 08:22:42

标签: android

我在Play商店发布的应用程序崩溃很少发生(<1%),我找不到原因。

应用程序在启动时崩溃,在Play商店控制台中,我使用以下堆栈跟踪记录了崩溃:

    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.xxx.xxx.MyApplication
    at com.xxx.xxx.activities.BaseActivity.getApplicationComponent(BaseActivity.java:83)
    at com.xxx.xxx.views.activities.BaseActivity.onCreate(BaseActivity.java:65)
    at com.xxx.xxx.views.activities.startup.StartupActivity.onCreate(StartupActivity.java:78)
    at android.app.Activity.performCreate(Activity.java:6664)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    ... 9 more

崩溃方法getApplicationComponent如下所示:

public ApplicationComponent getApplicationComponent() {
    return ((MyApplication) getApplication()).getApplicationComponent();
}

清单引用了我的自定义应用程序类:

<application
        android:name=".MyApplication"

该应用程序安装在几十万台设备(相同的apk)中,并且运行良好。

到目前为止,这似乎只发生在运行Nougat的一些Nexus 5X / 6P设备上。

我没有想法。有没有人知道为什么这只会发生几次?

修改

MyApplication类:

public class MyApplication extends Application {
    private ApplicationComponent applicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();

        this.applicationComponent = this.createApplicationComponentBuilder().build();
    }

    public ApplicationComponent getApplicationComponent() {
        return this.applicationComponent;
    }

    protected DaggerApplicationComponent.Builder createApplicationComponentBuilder() {
        return DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this));
    }
}

0 个答案:

没有答案