我在我的应用程序中使用 dagger2,我试图将 ViewModel 注入 BottomSheetDialogFragment 但我不知道如何。
我有这样的 BaseApplication 类:
public class BaseApplication extends DaggerApplication {
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent
.builder()
.application(this)
.build();
}
}
和 ViewModelFactory:
@Module
public abstract class ViewModelFactoryModule {
@Binds
public abstract ViewModelProvider.Factory mBindViewModelFactory(ViewModelProviderFactory mModelProviderFactory);
}
当我尝试在 BottomSheetDialogFragment 中注入视图模型时,它显示空异常
public class BottomSheetMoreOptions extends BottomSheetDialogFragment {
@Inject
FeedViewModel ViewModel;
}
ViewModel 构造函数是:
@Inject
public FeedViewModel(@Named("notificationsRef") DatabaseReference mRef) {
Log.d(TAG, "HomeViewModel: is ready...");
this.mRef = mRef;
}
实际上要加载底部工作表,我在其构造函数中传递了视图模型:
BottomSheetMoreOptions bottomSheetMoreOptions = new BottomSheetMoreOptions(model.getFeed().getId(), viewModel);
bottomSheetMoreOptions.show(requireActivity().getSupportFragmentManager(),
"ModalBottomSheet");
请在Java中有任何帮助吗?