2016-10-24 88 views
0

我一直無法自己重新創建它。這是Crashlytics by Fabric中出現的一次崩潰。匕首2崩潰:「不能從null @ Nullable @Provides方法返回null」

行號指向此: App.getApp().getApplicationComponent().inject(this);

我看不到我要去哪裏錯了。

「應用」是應用程序類,在我的測試中設備的清單和做工精細的延伸。它是這樣的:

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

有問題的崩潰片段具有標有@Inject註解的依賴。

這是一個完整的堆棧跟蹤:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com....../com.....activities.MainActivity}: java.lang.NullPointerException: Cannot return null from a [email protected] @Provides method 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7229) 
    at java.lang.reflect.Method.invoke(Method.java) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by java.lang.NullPointerException: Cannot return null from a [email protected] @Provides method 
    at dagger.internal.Preconditions.checkNotNull(Preconditions.java:48) 
    at com.....di.modules....Module_Provides...Factory.get(...Module_Provides...Factory.java:30) 
    at com......di.modules....Module_Provides...Factory.get(...Module_Provides...Factory.java:10) 
    at dagger.internal.DoubleCheck.get(DoubleCheck.java:47) 
    at com.....fragments.HomeFragment_MembersInjector.injectMembers(HomeFragment_MembersInjector.java:39) 
    at com.....fragments.HomeFragment_MembersInjector.injectMembers(HomeFragment_MembersInjector.java:9) 
    at com.....di.DaggerApplicationComponent.inject(DaggerApplicationComponent.java:217) 
    at com.....fragments.HomeFragment.onCreate(HomeFragment.java:87) 
    at android.support.v4.app.Fragment.performCreate(Fragment.java:2068) 
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1055) 
    at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838) 
    at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:878) 
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:719) 
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677) 
    at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:604) 
    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1265) 
    at android.app.Activity.performStart(Activity.java:6915) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3217) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7229) 
    at java.lang.reflect.Method.invoke(Method.java) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
+0

那麼你想要注入什麼東西,是'null'? – EpicPandaForce

+0

@EpicPandaForce我不知道。堆棧跟蹤不會說有2個依賴關係,它們都不應該爲null。他們都來自Ormlite數據庫。 – Questioner

+0

@EpicPandaForce如何讓一個類可以爲空?我試着運行它並用'@ Nullable'標記@提供方法,但它抱怨說這個類必須是可空的。我用'@ Nullable'標記了它,並沒有做任何事情。 – Questioner

回答

0

我只是通過返回一個默認的對象,而不是空的固定同樣的問題。我的錯誤始於相同的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; 
}