android – Dagger2错误:没有@Inject构造函数就无法提供

前端之家收集整理的这篇文章主要介绍了android – Dagger2错误:没有@Inject构造函数就无法提供前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我对Dagger 2全新,并且遇到一个小问题.希望你能帮我 :)
我的android项目中有以下类

> App
> AppComponent
> AppModule
> MainActivity
> MainComponent
> MainModule
> IntentStarter

在重建/编译时我得到错误

Error:(15,10) error: xyz.IntentStarter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
xyz..MainActivity.intentStarter
[injected field of type: xyz..IntentStarter intentStarter]

我尝试了很多变种但没有成功……我在IntentStarter类中使用构造函数尝试了它..没有构造函数……:/
现在一些代码……

// AppComponent.class
@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
// Empty...
}

// AppModule.class
@Singleton
@Module
public class AppModule {

    Application application;
    Context context;


    public AppModule(Application app) {
        this.application = app;
        this.context = app.getApplicationContext();
    }


    @Provides
    public Application provideApplication() {
        return application;
    }

    @Provides
    public Context provideContext() {
        return context;
    }

    @Provides
    public EventBus provideEventBus() {
        return EventBus.getDefault();
    }

    @Provides
    public IntentStarter provideIntentStarter() {
        return new IntentStarter();
    }
}

// App.class
public class App extends Application {

    public AppComponent appComponent;

    public AppComponent getAppComponent() {
        return appComponent;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        appComponent = DaggerAppComponent.builder()
            .appModule(new AppModule(this))
            .build();
    }
}

//MainAcitivty.class
public class MainActivity extends MosbyActivity {

    @Inject
    IntentStarter intentStarter;

    MainActivityComponent component;

    @Override
    protected void injectDependencies() {
        component = DaggerMainActivityComponent.builder()
                .appComponent(((App) getApplication()).getAppComponent())
                .build();
        component.inject(this);
    }
}

//MainActivityComponent.class
@ActivityScope
@Component(dependencies = {AppComponent.class})
public interface MainActivityComponent {
    void inject(MainActivity mainActivity);
}

// MainActivityModule
@Module
public class MainActivityModule {

}

//IntentStarter
public class IntentStarter {

    @Inject
    Context context;

}
最佳答案
首先,正如我所说,您的组件中缺少您的提供方法.例如,

 @Component(modules={AppModule.class})
 @Singleton
 public interface AppComponent {
        Context context();
        IntentStarter intentStarter();
 }

 @Component(dependencies={AppComponent.class}),modules={MainActivityModule.class})
 @ActivityScope
 public interface MainActivityComponent extends AppComponent {
        //other provision methods
 }

你在使用字段注入时犯了一个错误,你的IntentStarter需要调用appComponent.inject(this)或者需要在构造函数参数中获取你的上下文.

此外,我认为@Provides带注释的方法需要@Singleton范围来获取范围提供者,并且标记模块本身实际上并没有做任何事情.

所以,具体来说,

@Singleton
@Component(modules = {AppModule.class})
    public interface AppComponent {
    Application application();
    Context provideContext();
    EventBus provideEventBus();
    IntentStarter provideIntentStarter();
}

@Module
public class AppModule {
    Application application;
    Context context;

    public AppModule(Application app) {
        this.application = app;
        this.context = app.getApplicationContext();
    }


    @Provides
    public Application provideApplication() {
        return application;
    }

    @Provides
    public Context provideContext() {
        return context;
    }

    @Provides
    @Singleton
    public EventBus provideEventBus() {
        return EventBus.getDefault();
    }

    @Provides
    @Singleton
    public IntentStarter provideIntentStarter(Context context) {
        return new IntentStarter(context);
    }
}

@ActivityScope
@Component(dependencies = {AppComponent.class},modules={MainActivityModule.class})
public interface MainActivityComponent extends AppComponent {
    void inject(MainActivity mainActivity);
}

public class IntentStarter {    
    private Context context;

    public IntentStarter(Context context) {
        this.context = context;
    }   
}
原文链接:https://www.f2er.com/android/431317.html

猜你在找的Android相关文章