匕首2不生成组件类

时间:2017-08-09 14:19:30

标签: android dagger-2 dagger

我尝试在我的某个应用中首次使用匕首2。我在dagger组件类中遇到错误,因为没有生成类。请帮助我让它工作。

链接到我的应用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

这是我在dagger中的Component类

   @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..
}

我可以知道我的应用出了什么问题吗?

4 个答案:

答案 0 :(得分:1)

当您希望Dagger 2为您提供类时,请确保使用@Inject注释对构造函数进行注释:

@Inject
public DashboardInteractorImpl(DashboardPresenterImpl mDashboardPresenter){
    this.mDashboardPresenter=mDashboardPresenter;

    dHelper = new DatabaseHelper(MyApp.getContext());
    sManager= new SessionManagement(MyApp.getContext());
}

另外,摆脱注入应用程序的行:

@Singleton @Component(modules = AppModule.class)
public interface AppComponent {
   void inject(MyApp myApp);

   //delete the line below:
   //void inject(DashboardInteractorImpl dashboardInteractorImpl);
}

您还需要确保Dagger 2可以通过使用DashboardPresenterImpl为演示者注释构造函数或在模块中编写@Inject方法来提供@Provides

答案 1 :(得分:0)

尝试使用以下修改

// Component
@Singleton @Component(modules = AppModule.class)
public interface AppComponent {
    void inject(MyApp myApp);

    DashboardInteractorImpl getDashboardInteractorImpl();
}

// Module
@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(context);
    } 
    @Provides @Singleton 
    DatabaseHelper getDhelper(Context context){
        return new DatabaseHelper(context);
    } 
}

// Application
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;
    } 
} 

// DashboardInteractorImpl
public class DashboardInteractorImpl implements DashboardInteractor {

    private final DatabaseHelper dHelper;
    private final SessionManagement sManager;
    private DashboardPresenterImpl mDashboardPresenter;

    @Inject 
    public DashboardInteractorImpl(DatabaseHelper databaseHelper,
                SessionManagement sessionManagement
                DashboardPresenterImpl dashboardPresenter){

        dHelper = databaseHelper;
        sManager = sessionManagement;
        mDashboardPresenter = dashboardPresenter;
    }
     //SOME LOGIC HERE..
}

// DashboardPresenterImpl
public class DashboardPresenterImpl implements DashboardPresenter {

    @Inject     
    public DashboardPresenterImpl(ABC abc){
        //make sure ABC is provided by the module or inject on constructed
    }
}

答案 2 :(得分:0)

您可以通过两种方式注入DashboardInteractorImpl

您必须在App模块中声明DashboardInteractorImpl依赖关系。

或者

您可以编写@Inject构造函数。 我没有看到任何完全填充此依赖关系的代码。 所以你可以做两件事 在模块中提供此DashboardPresenterImpl, DashboardInteractorImpl 要么 为此类@InjectDashboardPresenterImpl

中的每一个创建DashboardInteractorImpl构造函数

答案 3 :(得分:0)

如果您在Kotlin中使用或编写代码,请将此行代码添加到build.gradle

apply plugin: 'kotlin-kapt'