2017-06-22 52 views
1

我有一個名爲ViewModelModule的模塊。這是它:無法從非@Nullable @Provides方法錯誤返回null

@Module(includes = RepositoryModule.class) 
public class ViewModelModule { 

    StepListFragment stepListFragment; 

    StepViewFragment stepViewFragment; 

    @Provides 
    public StepListFragment getStepListFragment() { 
     return stepListFragment; 
    } 

    @Provides 
    public StepViewFragment getStepViewFragment() { 
     return stepViewFragment; 
    } 

    public ViewModelModule(StepListFragment stepListFragment) { 
     this.stepListFragment = stepListFragment; 
    } 

    public ViewModelModule(StepViewFragment stepViewFragment) { 
     this.stepViewFragment = stepViewFragment; 
    } 

    @Provides 
    IngredientsViewModel ingredientsViewModel(StepListFragment stepListFragment, RecipesRepository repository) { 
     return ViewModelProviders.of(stepListFragment, new ViewModelProvider.Factory() { 
      @Override 
      public <T extends ViewModel> T create(Class<T> modelClass) { 
       return (T) new IngredientsViewModel(repository); 
      } 
     }).get(IngredientsViewModel.class); 
    } 

    @Provides 
    StepsViewModel stepsViewModel(StepViewFragment stepViewFragment, RecipesRepository repository) { 
     return ViewModelProviders.of(stepViewFragment, new ViewModelProvider.Factory() { 
      @Override 
      public <T extends ViewModel> T create(Class<T> modelClass) { 
       return (T) new StepsViewModel(repository); 
      } 
     }).get(StepsViewModel.class); 
    } 

} 

有我所有的ViewModules組件將被提供。但不是在同一時刻。

我會爲每個片段一個組件:

@Component(modules = {RepositoryModule.class, ContextModule.class, ViewModelModule.class}) 
public interface StepListComponent { 
    void inject (StepListFragment stepListFragment); 
} 



@Component(modules = {RepositoryModule.class, ContextModule.class, ViewModelModule.class}) 

public interface StepViewComponent { 
    void inject (StepViewFragment stepViewFragment); 
} 

在第一時刻StepListFragment的表現和我實例化的組件如下:

DaggerStepListComponent.builder() 
     .applicationModule(new ApplicationModule(this.getActivity().getApplication())) 
     .contextModule(new ContextModule(this.getActivity())) 
     .viewModelModule(new ViewModelModule(this)).build().inject(this); 

如您在條款的最後見我注入片段。

之後,當我開始另一個片段時,我會做同樣的事情。但是,當它調用上面的代碼,我收到此錯誤:

Caused by: java.lang.NullPointerException: Cannot return null from a [email protected] @Provides method 

關過程中的錯誤原因是,我還沒有實例化StepViewFragment stepViewFragment之中;但我現在不想使用它,所以它不會造成問題。

我嘗試添加下面的@Nullable條款:

@Nullable 
@Provides 
public StepListFragment getStepListFragment() { 
    return stepListFragment; 
} 

@Nullable 
@Provides 
public StepViewFragment getStepViewFragment() { 
    return stepViewFragment; 
} 

但後來我得到一個編譯時錯誤:

Error:(15, 10) error: StepListFragment is not nullable, but is being provided by @android.support.annotation.Nullable @Provides 
com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.steps.StepListFragment com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.di.ViewModelModule.getStepListFragment() 
    at:  com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.steps.StepListFragment is injected at 
    com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.di.ViewModelModule.ingredientsViewModel(stepListFragment, …) 
    com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.ingredients.IngredientsViewModel is injected at 
    com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.steps.StepListFragment.ingredientsViewModel 
    com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.steps.StepListFragment is injected at 
    com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.di.StepListComponent.inject(stepListFragment) 

所以現在的問題是:有沒有用,解決此問題的方法使用相同的模塊保存配置,還是應該將每個@Provides分隔到各自的模塊中?這是一個很好的做法嗎?

回答

1

片段不應該是您的ViewModel的依賴關係 - ViewModel應該比片段的範圍更大。

請參閱this GitHub repo與採用Android架構的組件用匕首2

+0

我片段和活動注入目標的示例項目,但我必須爲他們提供初始化需要它的模塊對象。這是ViewModels的情況。該片段只在內部提供模塊... – alexpfx

+0

@alexpfx請看看[這個答案](https://stackoverflow.com/q/44270577/5241933),特別是鏈接到[樣本GitHub項目]( https://github.com/googlesamples/android-architecture-components) –

+0

不錯的例子。我會稍後再研究它。 – alexpfx