我开始使用匕首,非常喜欢它,但现在面临一些困难。我的场景如下:有一个活动和一个依赖项。依赖项被注入到活动中,并且需要对该活动的引用。像这样:
public class MainActivity extends BaseActivity {
@Inject ScbeHelper scbeHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
}
public class ScbeHelper {
protected static final String TAG = "scbe_helper";
private BaseActivity activityContext;
@Inject
public ScbeHelper(BaseActivity context) {
this.activityContext = context;
}
}
我正在关注github 中 Dagger 的示例以获取活动图表。所以我在我的项目中创建了一个类似的结构。首先,继承BaseActivity
自的类:MainActivity
public abstract class BaseActivity extends Activity {
private ObjectGraph activityGraph;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
protoApp application = (protoApp) getApplication();
// Exception happens in next line, inside plus() method
activityGraph = application.getApplicationGraph().plus(getModules().toArray());
// Inject ourselves so subclasses will have dependencies fulfilled when this method returns.
activityGraph.inject(this);
((protoApp)getApplication()).inject(this);
}
protected List<Object> getModules() {
return Arrays.<Object>asList(new ActivityModule(this));
}
public void inject(Object object) {
activityGraph.inject(object);
}
}
和模块:
@Module(injects={MainActivity.class})
public class ActivityModule {
private final BaseActivity activity;
public ActivityModule(BaseActivity activity) {
this.activity = activity;
}
@Provides @Singleton BaseActivity provideActivity() {
return activity;
}
}
现在,问题: No injectable members on com.example.proto.BaseActivity. Do you want to add an injectable constructor? required by public com.example.proto.ScbeHelper(com.example.proto.BaseActivity)
换句话说,提供者方法由于某种原因ActivityModule.provideActivity()
并没有真正提供实例BaseActivity
,尽管在我的理解中它设置正确。有人在我的设置中看到错误吗?我在匕首的逻辑中遗漏了什么吗?
提前致谢!