【问题标题】:ClassCastException while injecting dependencies with dagger使用匕首注入依赖项时发生 ClassCastException
【发布时间】: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 这可能是您的解决方案。

【问题讨论】:

标签: android dagger-2 dagger


【解决方案1】:

这与Dagger无关。问题出在这里:

return (MyApplication) context.getApplicationContext();

getApplicationContext() 返回的Context保证是您的Application 实例。我遇到的唯一情况不是在模拟器中,但总是有可能的。

我更喜欢这种方法:

private static MyApplication gInstance;

@Override
public void onCreate() {
    gInstance = this;
}

public static MyApplication instance() {
    return gInstance;
}

这是安全的,因为 Application 实例已创建,并且其 onCreate 在创建任何其他 Android 组件之前被调用。

【讨论】:

  • 感谢您的建议。不幸的是,此更改导致 NullpointerExceptions 访问该实例。这怎么可能? ://
  • @chrjs 您是否在清单中设置了自定义应用程序类?您是否在生命周期方法之外调用 instance()(例如,在字段初始化程序中?)
  • 是的,它在清单中设置。并且只能通过 BaseActivity 的onCreate 访问
  • 所以BaseActivity 中的代码现在看起来像MyApplication.instance().getAppComponent().inject(this);?您是否尝试过对其进行分解以查看哪个方法返回 null?
  • 没错。我的问题是,我无法在我的设备上重现此问题。实例总是在 Activity 中调用 get 之前设置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 2015-11-04
相关资源
最近更新 更多