我正在尝试使用片段创建一个应用程序。我创建了一个测试片段(HomeFragment),它只有一个简单的TextView。我创建了所有必需的类(模块,模型和提供程序)。但是我收到一个奇怪的并发症错误
Error:(25, 10) error: [dagger.android.AndroidInjector.inject(T)] android.arch.lifecycle.ViewModelProvider.Factory is bound multiple times:
@Provides android.arch.lifecycle.ViewModelProvider.Factory app.series.com.series3go.ui.main.MainActivityModule.mainViewModelProvider(app.series.com.series3go.ui.main.MainViewModel)
@Provides android.arch.lifecycle.ViewModelProvider.Factory app.series.com.series3go.ui.home.HomeFragmentModule.provideHomeFragmentViewModel(app.series.com.series3go.ui.home.HomeFragmentViewModel)
家庭碎片
public class HomeFragment extends BaseFragment<HomeFragmentBinding, HomeFragmentViewModel> {
public static final String TAG = HomeFragment.class.getSimpleName();
@Inject
ViewModelProvider.Factory mViewModelFactory;
HomeFragmentBinding mHomeFragmentBinding;
private HomeFragmentViewModel mHomeFragmentViewModel;
public static HomeFragment newInstance() {
Bundle args = new Bundle();
HomeFragment fragment = new HomeFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mHomeFragmentBinding = getViewDataBinding();
setUp();
subscribeToLiveData();
}
@Override
public HomeFragmentViewModel getViewModel() {
mHomeFragmentViewModel = ViewModelProviders.of(this, mViewModelFactory).get(HomeFragmentViewModel.class);
return mHomeFragmentViewModel;
}
@Override
public int getBindingVariable() {
return BR.viewModel;
}
@Override
public int getLayoutId() {
return R.layout.home_fragment;
}
private void setUp() {
}
private void subscribeToLiveData() {
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
}
HomeFragment模块
@Module
public class HomeFragmentModule
{
@Provides
HomeFragmentViewModel homeFragmentViewModel()
{
return new HomeFragmentViewModel();
}
@Provides
ViewModelProvider.Factory provideHomeFragmentViewModel(HomeFragmentViewModel homeFragmentViewModel)
{
return new ViewModelProviderFactory<>(homeFragmentViewModel);
}
}
HomeFragmentProvider
@Module
public abstract class HomeFragmentProvider {
@ContributesAndroidInjector(modules = HomeFragmentModule.class)
abstract HomeFragment provideHomeFragmentFactory();
}
HomeFragmentViewModel
公共类HomeFragmentViewModel扩展BaseViewModel {
private final ObservableField<String> appVersion = new ObservableField<>();
public HomeFragmentViewModel() {
super();
appVersion.set("123");
}
public ObservableField<String> getAppVersion() {
return appVersion;
}
}
home_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="app.series.com.series3go.ui.main.MainActivity">
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type="app.series.com.series3go.ui.home.HomeFragmentViewModel" />
</data>
<TextView
android:id="@+id/tvAppVersion"
style="@style/TextStyle.Title.Sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:padding="5dp"
android:text="@{viewModel.appVersion}" />
</layout>
提供程序类(HomeFragmentProvider)中是否有关于函数名称的命名约定,
我在项目中的任何地方都没有看到这些功能的任何使用。它们用于生成的匕首类中吗?
谢谢
更新
AppComponent
@Singleton
@Component(modules = {AndroidSupportInjectionModule.class, AppModule.class, ActivityBuilder.class})
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
AppComponent build();
}
void inject(SeriesApp app);
}
最佳答案
看起来您的依赖图中有两个提供ViewModelProvider.Factory
的方法。
要解决这种歧义,请使用@Named
批注。
@Module
public class HomeFragmentModule {
@Provides
@Named("HomeFragment")
ViewModelProvider.Factory provideHomeFragmentViewModel(HomeFragmentViewModel homeFragmentViewModel) {
return new ViewModelProviderFactory<>(homeFragmentViewModel);
}
/* Rest of the code */
}
public class HomeFragment {
@Inject
@Named("HomeFragment")
ViewModelProvider.Factory mViewModelFactory;
/* Rest of the code */
}
您也应该对第二个
ViewModelProvider.Factory
执行相同的操作(在MainActivity
及其模块中)。