java.lang.IllegalArgumentException:Class<MyActivity_> 没有喷油器工厂
java.lang.IllegalArgumentException: No injector factory bound for Class<MyActivity_>
我遇到的错误如下:
Caused by: java.lang.IllegalArgumentException: No injector factory
bound for Class. Injector factories were bound for
supertypes of MyActivity_: [MyActivity]. Did you mean to bind an
injector factory for the subtype?
据我所知,这是因为我使用的是 AndroidAnnotations 库。
AppComponent.class :
@Singleton
@Component(modules = {
AndroidInjectionModule.class,
AppModule.class,
ActivityBindingModule.class })
public interface AppComponent extends AndroidInjector<DaggerApplication> {
@Component.Builder
interface Builder {
@BindsInstance Builder application(Application application);
AppComponent build();
}
void inject(Application application);
@Override
void inject(DaggerApplication instance);
}
AppModule.class :
@Module
public abstract class AppModule {
@Binds
abstract Context provideContext(Application application);
@Provides
@Singleton
static SharedPreferencesManager providesPreferences(Application application){
return SharedPreferencesManager_.getInstance_(application);
}
}
ActivityBindingModule.class :
@Module
public abstract class ActivityBindingModule {
@ContributesAndroidInjector(modules = LoginActivityModule.class)
@LoginActivityScope
abstract LoginActivity bindLoginActivity();
}
Application.class :
@EApplication
public class Application extends DaggerApplication {
@Inject
DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector;
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
AppComponent appComponent = DaggerAppComponent.builder()
.application(this)
.build();
appComponent.inject(this);
return appComponent;
}
}
LoginActivityModule.class
@Module
public class LoginActivityModule {
@Provides
@LoginActivityScope
@ActivityContext
public Context providesContext(LoginActivity loginActivity){
return loginActivity;
}
@Provides
@LoginActivityScope
public LoginViewModel providesLoginViewModel(TelephonyManager telephonyManager,
LoginModel loginModel,
SharedPreferencesManager sharedPreferencesManager,
LoginRemoteRepository loginRemoteRepository){
return new LoginViewModel(telephonyManager, loginModel, sharedPreferencesManager, loginRemoteRepository,
new CompositeSubscription());
}
@Provides
@LoginActivityScope
public LoginRemoteRepository providesRemoteRepository(@ActivityContext Context context,
MainApi mainApi,
SharedPreferencesManager sharedPreferencesManager){
return new LoginRemoteRepository(mainApi, sharedPreferencesManager, context.getContentResolver());
}
@Provides
@LoginActivityScope
public LoginModel provideLoginModel(){
return new LoginModel();
}
@Provides
@LoginActivityScope
public TelephonyManager provideTelephonyManager(Context context){
return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
}
}
LoginActivity.class :
@EActivity(R.layout.activity_login)
public class LoginActivity {
@Inject
LoginViewModel loginViewModel;
@AfterViews
void afterViews(){
AndroidInjection.inject(this);
}
}
只是一个明智的猜测:这可能是因为当您使用 AndroidAnnotations(他们在某处添加了下划线)时,您的 Class 实际上被称为不同的。然后你必须像这样定义绑定(不确定下划线在哪里):
@ContributesAndroidInjector(modules = LoginActivityModule_.class)
@LoginActivityScope
abstract LoginActivity bindLoginActivity();
如何处理 Dagger 2 错误:
"Injector factories were bound for supertypes of ... Did you mean to bind an injector factory for the subtype?"
假设我们有一些 BaseActivity:
open class BaseActivity : MvpAppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
}
}
我们也有一个 ActivityBuilder 模块(它提供 BaseActivity 本身)并且我们有一个模块提供
成功 运行 BaseActivity 所需的依赖项。
@Module
abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = [BaseModule::class])
abstract fun bindBaseActivity(): BaseActivity
}
如果我们像这样添加一个 ChildActivity
class ChildActivity : BaseActivity() {
}
思考:“嘿,Dagger 2 将满足 BaseActivity 的依赖关系,因为我们正在扩展
它,我们将获得一个 ChildActivity 并 运行,对吗?”。错误。我们将获得异常 "Injector factories were bound for supertypes of ... Did you mean to bind an injector factory for the subtype?"
- 我们应该做什么?我们应该让 Dagger 2 明确知道我们的 ChildActivity。
a) 添加AndroidInjection.inject(this)到ChildActivity的onCreate()方法:
class ChildActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
}
}
b) 将 BaseActivity 的所有子class 添加到 ActivityBuilder 模块(提供活动)。
请注意,还应包含满足 BaseActivity 依赖关系的 BaseModule
到 child class (ChildActivity)
的 @ContributesAndroidInjector
@Module
abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = [BaseModule::class])
abstract fun bindBaseActivity(): BaseActivity
//Adding subclasses
@ContributesAndroidInjector(modules = [BaseModule::class])
abstract fun bindChildActivity(): ChildActivity
}
我遇到的错误如下:
Caused by: java.lang.IllegalArgumentException: No injector factory bound for Class. Injector factories were bound for supertypes of MyActivity_: [MyActivity]. Did you mean to bind an injector factory for the subtype?
据我所知,这是因为我使用的是 AndroidAnnotations 库。
AppComponent.class :
@Singleton
@Component(modules = {
AndroidInjectionModule.class,
AppModule.class,
ActivityBindingModule.class })
public interface AppComponent extends AndroidInjector<DaggerApplication> {
@Component.Builder
interface Builder {
@BindsInstance Builder application(Application application);
AppComponent build();
}
void inject(Application application);
@Override
void inject(DaggerApplication instance);
}
AppModule.class :
@Module
public abstract class AppModule {
@Binds
abstract Context provideContext(Application application);
@Provides
@Singleton
static SharedPreferencesManager providesPreferences(Application application){
return SharedPreferencesManager_.getInstance_(application);
}
}
ActivityBindingModule.class :
@Module
public abstract class ActivityBindingModule {
@ContributesAndroidInjector(modules = LoginActivityModule.class)
@LoginActivityScope
abstract LoginActivity bindLoginActivity();
}
Application.class :
@EApplication
public class Application extends DaggerApplication {
@Inject
DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector;
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
AppComponent appComponent = DaggerAppComponent.builder()
.application(this)
.build();
appComponent.inject(this);
return appComponent;
}
}
LoginActivityModule.class
@Module
public class LoginActivityModule {
@Provides
@LoginActivityScope
@ActivityContext
public Context providesContext(LoginActivity loginActivity){
return loginActivity;
}
@Provides
@LoginActivityScope
public LoginViewModel providesLoginViewModel(TelephonyManager telephonyManager,
LoginModel loginModel,
SharedPreferencesManager sharedPreferencesManager,
LoginRemoteRepository loginRemoteRepository){
return new LoginViewModel(telephonyManager, loginModel, sharedPreferencesManager, loginRemoteRepository,
new CompositeSubscription());
}
@Provides
@LoginActivityScope
public LoginRemoteRepository providesRemoteRepository(@ActivityContext Context context,
MainApi mainApi,
SharedPreferencesManager sharedPreferencesManager){
return new LoginRemoteRepository(mainApi, sharedPreferencesManager, context.getContentResolver());
}
@Provides
@LoginActivityScope
public LoginModel provideLoginModel(){
return new LoginModel();
}
@Provides
@LoginActivityScope
public TelephonyManager provideTelephonyManager(Context context){
return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
}
}
LoginActivity.class :
@EActivity(R.layout.activity_login)
public class LoginActivity {
@Inject
LoginViewModel loginViewModel;
@AfterViews
void afterViews(){
AndroidInjection.inject(this);
}
}
只是一个明智的猜测:这可能是因为当您使用 AndroidAnnotations(他们在某处添加了下划线)时,您的 Class 实际上被称为不同的。然后你必须像这样定义绑定(不确定下划线在哪里):
@ContributesAndroidInjector(modules = LoginActivityModule_.class)
@LoginActivityScope
abstract LoginActivity bindLoginActivity();
如何处理 Dagger 2 错误: "Injector factories were bound for supertypes of ... Did you mean to bind an injector factory for the subtype?"
假设我们有一些 BaseActivity:
open class BaseActivity : MvpAppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) } }
我们也有一个 ActivityBuilder 模块(它提供 BaseActivity 本身)并且我们有一个模块提供 成功 运行 BaseActivity 所需的依赖项。
@Module
abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = [BaseModule::class])
abstract fun bindBaseActivity(): BaseActivity
}
如果我们像这样添加一个 ChildActivity
class ChildActivity : BaseActivity() { }
思考:“嘿,Dagger 2 将满足 BaseActivity 的依赖关系,因为我们正在扩展 它,我们将获得一个 ChildActivity 并 运行,对吗?”。错误。我们将获得异常 "Injector factories were bound for supertypes of ... Did you mean to bind an injector factory for the subtype?"
- 我们应该做什么?我们应该让 Dagger 2 明确知道我们的 ChildActivity。
a) 添加AndroidInjection.inject(this)到ChildActivity的onCreate()方法:
class ChildActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
}
}
b) 将 BaseActivity 的所有子class 添加到 ActivityBuilder 模块(提供活动)。 请注意,还应包含满足 BaseActivity 依赖关系的 BaseModule 到 child class (ChildActivity)
的 @ContributesAndroidInjector@Module
abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = [BaseModule::class])
abstract fun bindBaseActivity(): BaseActivity
//Adding subclasses
@ContributesAndroidInjector(modules = [BaseModule::class])
abstract fun bindChildActivity(): ChildActivity
}