Cannot return null from a non-@Nullable @Provides 方法错误

Cannot return null from a non-@Nullable @Provides method error

我有一个名为 ViewModelModule 的模块。就是这样:

@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 non-@Nullable @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 分开到各自的模块中?这是一个好习惯吗?

片段不应该是您 ViewModel 的依赖项 - ViewModel 应该比 Fragment 具有更大的范围。

请参阅 this GitHub repo 以及使用 Android architecture componentsDagger2 的示例项目。