【发布时间】:2018-01-17 12:00:21
【问题描述】:
我第一次尝试在我的一个应用中使用 dagger 2。我在匕首组件类中遇到错误,因为该类没有生成。请帮我让它工作。
链接到我的应用https://github.com/kantigaricharan/Zolo
这是错误
02:45:55.663 [ERROR] [system.err] /Users/vamsikrishna/Downloads/Zolo/app/src/main/java/com/example/saicharan/zolo/dagger/component/AppComponent.java:17: error: com.example.saicharan.zolo.dashboard.DashboardInteractorImpl cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
02:45:55.663 [ERROR] [system.err] void inject(MyApp myApp);
02:45:55.663 [ERROR] [system.err] ^
02:45:55.663 [ERROR] [system.err] com.example.saicharan.zolo.dashboard.DashboardInteractorImpl is injected at
02:45:55.663 [ERROR] [system.err] com.example.saicharan.zolo.MyApp.dashboardInteractor
02:45:55.664 [ERROR] [system.err] com.example.saicharan.zolo.MyApp is injected at
02:45:55.664 [ERROR] [system.err] com.example.saicharan.zolo.dagger.component.AppComponent.inject(myApp)
02:45:55.722 [ERROR] [system.err] 1 error
这是我在匕首中的组件类
@Singleton @Component(modules = AppModule.class)
public interface AppComponent {
void inject(MyApp myApp);
void inject(DashboardInteractorImpl dashboardInteractorImpl);
}
这是模块
@Module
public class AppModule {
private final MyApp myApp;
public AppModule(MyApp myApp){this.myApp=myApp;}
@Provides @Singleton
Context providesApplicationContext(){
return myApp;
}
@Provides @Singleton
SessionManagement getSession(Context context){
return new SessionManagement(myApp);
}
@Provides @Singleton
DatabaseHelper getDhelper(Context context){
return new DatabaseHelper(myApp);
}
}
应用类
public class MyApp extends Application {
protected AppComponent appComponent;
private static MyApp instance;
@Inject
DashboardInteractorImpl dashboardInteractor;
public static MyApp getInstance() {
return instance;
}
public static Context getContext(){
// return (MyApp) context.getApplicationContext();
return instance.getApplicationContext();
}
@Override
public void onCreate() {
instance = this;
super.onCreate();
appComponent = DaggerAppComponent
.builder()
.appModule(new AppModule(this))
.build();
appComponent.inject(this);
}
public AppComponent getAppComponent(){
return appComponent;
}
}
DasboardInteractorImpl
public class DashboardInteractorImpl implements DashboardInteractor {
private final DatabaseHelper dHelper;
private final SessionManagement sManager;
DashboardPresenterImpl mDashboardPresenter;
public DashboardInteractorImpl(DashboardPresenterImpl mDashboardPresenter){
this.mDashboardPresenter=mDashboardPresenter;
dHelper = new DatabaseHelper(MyApp.getContext());
sManager= new SessionManagement(MyApp.getContext());
}
//SOME LOGIC HERE..
}
我能知道我的应用出了什么问题吗?
【问题讨论】:
-
您在创建模块和组件后是否重新构建了项目?
-
去掉应用插件:'com.neenbedankt.android-apt' - 如果可能的话,你所有的
apt都应该替换为annotationProcessor -
@eurosecom 是的,我已经这样做了,但仍然没有解决错误
-
@DavidRawson 当我尝试同步 gradle 时,它现在给 Gradle dsl 方法找不到合适的方法
-
你仍然有
apt用于 ButterKnifeapt 'com.jakewharton:butterknife-compiler:8.8.0'- 检查 ButterKnife 页面以获取正确的行以放入 gradle