【发布时间】:2017-06-28 07:07:39
【问题描述】:
我正在使用 Dagger2 在我的所有应用程序中注入我的依赖项。
几天前,我开始收到来自三星 Android 7.0(仅限这些)设备的其中一个应用程序的崩溃报告。
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
..
Caused by: java.lang.ClassCastException:
at de.package.name.MyApplication.get(MyApplication.java:43)
at de.package.name.ui.base.BaseActivity.onCreate(BaseActivity.java:53)
at de.package.name.ui.startup.StartupActivity.onCreate(StartupActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
MyApplication 类:
public class MyApplication extends MultiDexApplication {
private AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
setupAppComponent();
}
private void setupAppComponent() {
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.userApiModule(new UserApiModule())
.build();
appComponent.inject(this);
}
public static MyApplication get(Context context) {
return (MyApplication) context.getApplicationContext();
}
}
BaseActivity 类的相关部分:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.get(this).getAppComponent().inject(this);
}
最后是 StartupActivity 部分:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupComponent(MyApplication.get(this).getAppComponent());
setContentView(R.layout.activity_startup);
startupPresenter.bindView(this);
}
public void setupComponent(AppComponent appComponent) {
startupComponent = DaggerStartupComponent.builder()
.appComponent(appComponent)
.startupModule(new StartupModule())
.build();
startupComponent.inject(this);
}
我已经将 Dagger 更新到了最新版本(目前为 2.11)。但我对这个问题没有任何想法。此外,我无法在我的三星 S8 7.0 设备上重现它。
所以如果你有任何想法,请告诉我!
干杯
编辑: 如果有人遇到这个问题。看这里:RuntimeException with Dagger 2 on Android 7.0 and Samsung devices 这可能是您的解决方案。
【问题讨论】: