我想知道如何使用代码在活动中注入Presenter以下是详细信息
以下是错误消息:
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.android.category_listing.CategoryContract.CategoryPresenter cannot be provided without an @Provides-annotated method.
com.headytest.android.category_listing.CategoryContract.CategoryPresenter is injected at
com.headytest.android.MainActivity.categoryPresenter
com.headytest.android.MainActivity is injected at
com.headytest.android.dagger_component.NetComponent.inject(com.headytest.android.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() {
}
}
答案 0 :(得分:1)
我认为当前的错误很容易解决,因为它看起来很像this。
您已指示Dagger如何提供@AScope CategoryContract.CategoryPresenter
,但在活动中您请求Dagger注射CategoryContract.CategoryPresenter
。 Dagger此时很困惑,因为这两件事情不匹配。
您需要做的只是将@AScope
添加到categoryPresenter
:
@Inject
@AScope
CategoryPresenter categoryPresenter;
我已查看your project。问题发生在NetComponent.java
:
@Singleton
@Component(modules = {ApplicationModule.class, NetworkModule.class})
public interface NetComponent {
void inject(MainActivity activity);
}
问题在于void inject(MainActivity activity)
。为什么这是一个问题?因为当您在App
中构建NetComponent
时,匕首会对MainActivity
注释字段@Inject
进行分析并看到CategoryPresenter
。这意味着,此时Dagger应该找到合适的provider / binder方法,以便能够在此接口中注入MainActivity
。
但它找不到这样的provider / binder方法,因为它是在另一个组件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
相同。
要解决此问题,您必须在Retrofit
中添加NetComponent
提供商方法(或从活动中删除@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
。