我正在尝试使用支持v4片段创建一个简单的dagger 2应用程序。 修改我的应用程序后,我得到了这个奇怪的编译错误
Error:(35, 8) error: [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends android.support.v4.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> cannot be provided without an @Provides-annotated method.
java.util.Map<java.lang.Class<? extends android.support.v4.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> is injected at
dagger.android.DispatchingAndroidInjector.<init>(injectorFactories)
dagger.android.DispatchingAndroidInjector<android.support.v4.app.Fragment> is injected at
app.series.com.tvshowsapplication.ui.main.MainActivity.fragmentDispatchingAndroidInjector
app.series.com.tvshowsapplication.ui.main.MainActivity is injected at
dagger.android.AndroidInjector.inject(arg0)
快速搜索后,我发现了link。它说我需要使用
事情是我不明白我应该把这部分放在哪里
@Multibinds
abstract Map<Class<? extends android.app.Fragment>, AndroidInjector.Factory<? extends android.app.Fragment>> bindNativeFragments();
我的猜测我需要将它放在BaseActivity中,因为它是一个抽象函数,但我如何在主要活动中实现它呢?
我的主要活动:
public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> implements MainNavigator, HasSupportFragmentInjector {
@Inject
ViewModelProvider.Factory mViewModelFactory;
@Inject
DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector;
private MainViewModel mMainViewModel;
private DrawerLayout mDrawer;
private Toolbar mToolbar;
private NavigationView mNavigationView;
// private SwipePlaceHolderView mCardsContainerView;
ActivityMainBinding mActivityMainBinding;
public static Intent getStartIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class);
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivityMainBinding = getViewDataBinding();
mMainViewModel.setNavigator(this);
setUp();
}
@Override
protected void onResume() {
super.onResume();
if (mDrawer != null)
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
public void onFragmentDetached(String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment != null) {
fragmentManager
.beginTransaction()
.disallowAddToBackStack()
.setCustomAnimations(R.anim.slide_left, R.anim.slide_right)
.remove(fragment)
.commitNow();
unlockDrawer();
}
}
private void setUp() {
mDrawer = mActivityMainBinding.drawerView;
mToolbar = mActivityMainBinding.toolbar;
mNavigationView = mActivityMainBinding.navigationView;
// mCardsContainerViewerView = mActivityMainBinding.cardsContainer;
setSupportActionBar(mToolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawer,
mToolbar,
R.string.open_drawer,
R.string.close_drawer) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
hideKeyboard();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
mDrawer.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
setupNavMenu();
String version = getString(R.string.version) + " " + BuildConfig.VERSION_NAME;
mMainViewModel.updateAppVersion(version);
mMainViewModel.onNavMenuCreated();
// setupCardContainerView();
subscribeToLiveData();
}
private void subscribeToLiveData() {
}
private void setupNavMenu() {
NavHeaderMainBinding navHeaderMainBinding = DataBindingUtil.inflate(getLayoutInflater(),
R.layout.nav_header_main, mActivityMainBinding.navigationView, false);
mActivityMainBinding.navigationView.addHeaderView(navHeaderMainBinding.getRoot());
navHeaderMainBinding.setViewModel(mMainViewModel);
mNavigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
mDrawer.closeDrawer(GravityCompat.START);
switch (item.getItemId()) {
// case R.id.navItemAbout:
// showAboutFragment();
// return true;
// case R.id.navItemRateUs:
// RateUsDialog.newInstance().show(getSupportFragmentManager());
// return true;
// case R.id.navItemFeed:
// startActivity(FeedActivity.getStartIntent(MainActivity.this));
// return true;
// case R.id.navItemLogout:
// mMainViewModel.logout();
// return true;
// default:
// return false;
}
return false;
}
});
}
// private void showAboutFragment() {
// lockDrawer();
// getSupportFragmentManager()
// .beginTransaction()
// .disallowAddToBackStack()
// .setCustomAnimations(R.anim.slide_left, R.anim.slide_right)
// .add(R.id.clRootView, AboutFragment.newInstance(), AboutFragment.TAG)
// .commit();
// }
private void lockDrawer() {
if (mDrawer != null)
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
private void unlockDrawer() {
if (mDrawer != null)
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
@Override
public void openLoginActivity() {
startActivity(LoginActivity.getStartIntent(this));
finish();
}
@Override
public void handleError(Throwable throwable) {
// handle error
}
@Override
public MainViewModel getViewModel() {
mMainViewModel = ViewModelProviders.of(this, mViewModelFactory).get(MainViewModel.class);
return mMainViewModel;
}
@Override
public int getBindingVariable() {
return BR.viewModel;
}
@Override
public int getLayoutId() {
return R.layout.activity_main;
}
@Override
public AndroidInjector<Fragment> supportFragmentInjector() {
return fragmentDispatchingAndroidInjector;
}
}
更新
我实际上在组件类中添加了模块,这是我的AppComponent
@Singleton
@Component(modules = {AndroidInjectionModule.class, AppModule.class, ActivityBuilder.class})
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
AppComponent build();
}
void inject(TVShowsApp app);
}
的AppModule
@Module
public class AppModule {
@Provides
@Singleton
Context provideContext(Application application) {
return application;
}
@Provides
@Singleton
CalligraphyConfig provideCalligraphyDefaultConfig() {
return new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/source-sans-pro/SourceSansPro-Regular.ttf")
.setFontAttrId(R.attr.fontPath)
.build();
}
}
和ActivityBuilder
@Module
public abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = MainActivityModule.class)
abstract MainActivity bindMainActivity();
}
答案 0 :(得分:7)
您需要将其放入模块中,然后在组件中注册,但您不必手动声明,因为Dagger已经拥有AndroidInjectionModule
(或AndroidSupportInjectionModule
您可以使用的AppCompat。
您可以在official documentation中找到有关Android设置的更多信息,或在最近询问的when you need to install the module类似问题中查看一些其他信息。
您没有展示AppComponent,但您很可能只是忘记添加模块,因此您可以通过将错误添加到组件来修复错误。
// add it somehow like this
@Component(modules = { AndroidSupportInjectionModule.class, AppModule.class })
public interface AppComponent {
void inject(App app);
}