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 楼答案
我假设当前的错误很容易修复,因为它看起来很像this问题
您已经指示Dagger如何提供
@AScope CategoryContract.CategoryPresenter
,但在活动中,您要求Dagger注入CategoryContract.CategoryPresenter
。Dagger在这一点上感到困惑,因为这两件事不匹配你要做的就是把
@AScope
添加到categoryPresenter
:我已经退房了。问题出在{}
问题在第^{行。为什么这是一个问题?因为当你在^{} 中构造
NetComponent
时,dagger会分析MainActivity
的@Inject
注释字段,并看到CategoryPresenter
。这意味着,在这一点上,Dagger应该找到合适的提供者/绑定器方法,以便能够注入MainActivity
,就像在这个接口中声明的那样但是它找不到这样的提供者/绑定器方法,因为它是在另一个组件中声明的-
CategoryPresenterComponent
,而这个组件(NetComponent
)无论如何都没有与CategoryPresenterComponent
连接(子组件或组件依赖),因此绑定不会公开只需删除该行即可使构建成功:
要解决“错误:无法访问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
):完成此操作后,您将能够运行该应用程序,但最终将在
MainActivity
中进入NPE,因为这行代码:您指的是尚未初始化的
retrofit
对象长话短说,你最初的问题已经变异了好几次,事实上你的代码中有一些问题。仍然存在一个问题,因为您试图使用
retrofit
来构造一个组件,该组件应该为您注入retrofit