有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过Dagger将演示者注入活动

我想知道如何使用以下代码在活动中注入演示者

以下是错误消息:

 Error:(12, 46) error: cannot find symbol class DaggerCategoryPresenterComponent
Error:(9, 46) error: cannot find symbol class DaggerNetComponent
Error:(18, 10) error: [Dagger/MissingBinding] com.headytest.安卓.category_listing.CategoryContract.CategoryPresenter cannot be provided without an @Provides-annotated method.
com.headytest.安卓.category_listing.CategoryContract.CategoryPresenter is injected at
com.headytest.安卓.MainActivity.categoryPresenter
com.headytest.安卓.MainActivity is injected at
com.headytest.安卓.dagger_component.NetComponent.inject(com.headytest.安卓.MainActivity)

以下是模块

  @Module
  public class NetworkModule {

String baseURL;

public NetworkModule(String baseURL) {
    this.baseURL = baseURL;
}

@Provides
@Singleton
Cache provideHttpCache(Application application) {
    int cacheSize = 10 * 1024 * 1024;
    Cache cache = new Cache(application.getCacheDir(), cacheSize);
    return cache;
}

@Provides
@Singleton
Gson provideGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return gsonBuilder.create();
}

@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache) {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.cache(cache);
    client.addInterceptor(interceptor);
    return client.build();
}

@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
    try {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(baseURL)
                .client(okHttpClient)
                .build();
    } catch (Exception e) {
        e.printStackTrace();
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(Constants.BASE_URL)
                .client(okHttpClient)
                .build();
    }
}
}

ApplicationModule

@Module
public class ApplicationModule {

Application application;

public ApplicationModule(Application application) {
    this.application = application;
}

@Provides
@Singleton
Application providesApplication() {
    return application;
}
}

CategoryContractModule

@Module
public class CategoryContractModule {


public CategoryContractModule() {
}

@Provides
@AScope
CategoryContract.CategoryPresenter providesCategoryPresenter(CategoryPresenterImpl categoryPresenter) {
    return (CategoryContract.CategoryPresenter)categoryPresenter;
}
}

以下是组件:

@Singleton
@Component(modules = {ApplicationModule.class, NetworkModule.class})
public interface NetComponent {
void inject(MainActivity activity);
 }

CategoryPresenterComponent

@AScope
@Component(dependencies = NetComponent.class, modules = 
{CategoryContractModule.class})
public interface CategoryPresenterComponent {
 void inject(MainActivity activity);
 }

AScope

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface AScope {
}

以下是MainActivity中的代码:

@Inject
CategoryPresenter categoryPresenter;

@Inject
Retrofit retrofit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     DaggerCategoryPresenterComponent.builder()
            .categoryContractModule(new CategoryContractModule())
            .netComponent(((App) getApplicationContext()).getNetComponent())
            .build()
            .inject(this);
}

CategoryPresenterImpl

    public class CategoryPresenterImpl implements CategoryContract.CategoryPresenter {

    @Inject
    public CategoryPresenterImpl() {
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void getCategoryLiast() {


    }
}

共 (1) 个答案

  1. # 1 楼答案

    我假设当前的错误很容易修复,因为它看起来很像this问题

    您已经指示Dagger如何提供@AScope CategoryContract.CategoryPresenter,但在活动中,您要求Dagger注入CategoryContract.CategoryPresenter。Dagger在这一点上感到困惑,因为这两件事不匹配

    你要做的就是把@AScope添加到categoryPresenter

    @Inject
    @AScope
    CategoryPresenter categoryPresenter;
    

    我已经退房了。问题出在{}

    @Singleton
    @Component(modules = {ApplicationModule.class, NetworkModule.class})
    public interface NetComponent {
        void inject(MainActivity activity);
    }
    

    问题在第^{行。为什么这是一个问题?因为当你在^{}中构造NetComponent时,dagger会分析MainActivity@Inject注释字段,并看到CategoryPresenter。这意味着,在这一点上,Dagger应该找到合适的提供者/绑定器方法,以便能够注入MainActivity,就像在这个接口中声明的那样

    但是它找不到这样的提供者/绑定器方法,因为它是在另一个组件中声明的-CategoryPresenterComponent,而这个组件(NetComponent)无论如何都没有与CategoryPresenterComponent连接(子组件或组件依赖),因此绑定不会公开

    只需删除该行即可使构建成功:

    @Singleton
    @Component(modules = {ApplicationModule.class, NetworkModule.class})
    public interface NetComponent {
    }
    

    要解决“错误:无法访问Nullable”的问题,请参阅this线程,它建议将compile 'com.google.code.findbugs:jsr305:3.0.2'应用于gradle文件

    完成此操作后,您将克服该错误消息,并将偶然发现retrofit2.Retrofit cannot be provided without an @Inject constructor or an @Provides-annotated method,它与CategoryPresenter有相同的症状

    要解决此问题,必须在NetComponent内添加Retrofit提供程序方法(或从活动中删除@Inject Retrofit retrofit):

    @Singleton
    @Component(modules = {ApplicationModule.class, NetworkModule.class})
    public interface NetComponent {
        Retrofit providesRetrofit();
    }
    

    完成此操作后,您将能够运行该应用程序,但最终将在MainActivity中进入NPE,因为这行代码:

    .categoryContractModule(new CategoryContractModule(retrofit.create(HeadyAPI.class)))
    

    您指的是尚未初始化的retrofit对象

    长话短说,你最初的问题已经变异了好几次,事实上你的代码中有一些问题。仍然存在一个问题,因为您试图使用retrofit来构造一个组件,该组件应该为您注入retrofit