Dagger:class 上没有多次注射的可注射成员
Dagger: No injectable members on class with multiple injections
我在尝试将 2 个模块注入 class 时收到错误消息:
Error:(18, 8) error: No injectable members on ...SQLiteHandler.
Do you want to add an injectable constructor? required by ...activity.AuthenticatorActivity
for ...ServerHandlerModule
Error:(18, 8) error: No injectable members on ...ServerHandler.
Do you want to add an injectable constructor? required by ....activity.AuthenticatorActivity
for ...Modules.SQLiteHandlerModule
我无法理解此错误消息,因为这两个模块似乎彼此没有任何关系。在我添加 ServerHandlerModule 之前注入工作正常,但是当它们都被注入时它们似乎相互干扰。
SQLiteHandlerModule:
@Module(injects = {AuthenticatorActivity.class, MainActivity.class})
public class SQLiteHandlerModule {
private final Context context;
public SQLiteHandlerModule(Context context) {
this.context = context;
}
@Provides
@Singleton
SQLiteHandler providesSQLiteHandler(@ForApplication Context context) {
return new SQLiteHandler(context);
}
@Provides
@Singleton
@ForApplication
Context provideApplicationContext() {
return context;
}
}
ServerHandlerModule:
@Module(injects = {AuthenticatorActivity.class})
public class ServerHandlerModule {
public ServerHandlerModule() {}
@Provides
@Singleton
ServerHandler providesServerHandler() {
return new ServerHandler();
}
}
AuthenticatorActivity:
public class AuthenticatorActivity extends AccountAuthenticatorActivity {
@Inject
SQLiteHandler db;
@Inject
ServerHandler serverHandler;
private ObjectGraph objectGraph;
@Override
protected void onCreate(Bundle savedInstanceState) {
objectGraph = ObjectGraph.create(getModules());
objectGraph.inject(this);
}
…
protected List<Object> getModules() {
return Arrays.asList(new SQLiteHandlerModule(this), new ServerHandlerModule());
}
}
有人知道我需要添加什么来解决这个问题吗?
Dagger 需要对要注入的 class 的构造函数进行注解。
@Inject
public SQLiteHandler(@ForApplication Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Inject
public ServerHandler() {
}
我在尝试将 2 个模块注入 class 时收到错误消息:
Error:(18, 8) error: No injectable members on ...SQLiteHandler.
Do you want to add an injectable constructor? required by ...activity.AuthenticatorActivity
for ...ServerHandlerModule
Error:(18, 8) error: No injectable members on ...ServerHandler.
Do you want to add an injectable constructor? required by ....activity.AuthenticatorActivity
for ...Modules.SQLiteHandlerModule
我无法理解此错误消息,因为这两个模块似乎彼此没有任何关系。在我添加 ServerHandlerModule 之前注入工作正常,但是当它们都被注入时它们似乎相互干扰。
SQLiteHandlerModule:
@Module(injects = {AuthenticatorActivity.class, MainActivity.class})
public class SQLiteHandlerModule {
private final Context context;
public SQLiteHandlerModule(Context context) {
this.context = context;
}
@Provides
@Singleton
SQLiteHandler providesSQLiteHandler(@ForApplication Context context) {
return new SQLiteHandler(context);
}
@Provides
@Singleton
@ForApplication
Context provideApplicationContext() {
return context;
}
}
ServerHandlerModule:
@Module(injects = {AuthenticatorActivity.class})
public class ServerHandlerModule {
public ServerHandlerModule() {}
@Provides
@Singleton
ServerHandler providesServerHandler() {
return new ServerHandler();
}
}
AuthenticatorActivity:
public class AuthenticatorActivity extends AccountAuthenticatorActivity {
@Inject
SQLiteHandler db;
@Inject
ServerHandler serverHandler;
private ObjectGraph objectGraph;
@Override
protected void onCreate(Bundle savedInstanceState) {
objectGraph = ObjectGraph.create(getModules());
objectGraph.inject(this);
}
…
protected List<Object> getModules() {
return Arrays.asList(new SQLiteHandlerModule(this), new ServerHandlerModule());
}
}
有人知道我需要添加什么来解决这个问题吗?
Dagger 需要对要注入的 class 的构造函数进行注解。
@Inject
public SQLiteHandler(@ForApplication Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Inject
public ServerHandler() {
}