Dagger 2崩溃:“无法从非@Nullable @Provides方法返回null”

时间:2016-10-24 09:22:30

标签: java android android-studio exception dagger-2

我自己无法重新创建它。这是一个崩溃,已经出现在Crashlytics by Fabric。

行号指向: char cBuff[100]; buffptr = cBuff; while (( nbytes = read(fd, buffptr, cBuff + sizeof(cBuff) - buffptr - 1)) > 0) { //Do something } if(nbytes < 0 ) { perror("Serial Read Thread ERROR:"); } else if(nbytes >= 0 ) { //Do something }

我看不出我出错的地方。

'App'是Application类的扩展,它位于清单中并且在我测试的设备上正常工作。就像这样:

App.getApp().getApplicationComponent().inject(this);

有问题的崩溃片段具有使用@Inject注释标记的依赖关系。

这是一个完整的堆栈跟踪:

applicationComponent = DaggerApplicationComponent.builder()
            .appModule(new AppModule(this))
...
...
            .netModule(new NetModule())
            .build();

1 个答案:

答案 0 :(得分:0)

我刚刚通过返回默认对象而不是null来修复了同样的问题。我的错误始于相同的Dagger消息。我以为我可以在注入的类中处理null。使所有依赖项不可为空是很有意义的,因为它们可以是其他依赖项的一部分。这是非常基础的,但当然在编码时我没有想到这一点。

@Provides
@Singleton
public FavCountry provideFavoriteCountry(RealmProvider realmProvider){

    FavCountry country = realmProvider.getRealm().where( FavCountry.class ).findFirst();

    //wait a minute, the table can be empty
    if( country == null ){
        country = new FavCountry("");
    }

    return country;
}
相关问题