尝试将2个模块注入类中时收到错误消息:
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());
}
}
有谁知道我需要添加什么才能解决这个问题?
答案 0 :(得分:0)
Dagger需要对要注入的类的构造函数进行注释。
@Inject
public SQLiteHandler(@ForApplication Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Inject
public ServerHandler() {
}