我正在尝试使用片段创建一个应用程序。我创建了一个测试片段(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)
HomeFragment
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();
}
}
HomeFragmentModule
@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)中,函数命名是否有约定?我没有看到项目中任何地方使用这些函数。它们在dagger生成的类中使用吗?
谢谢!
更新
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);
}