我正在使用Android Dagger2,但出现类似错误
x=r['class_ids']
y=r1['class_ids']
z=np.array_equal(x,y)
import collections
pic_before=collections.Counter(x)
pic_after=collections.Counter(y)
import pandas as pd
mydict = { 'pic after uploading ':pic_before, 'pic just before sending':pic_after, 'match':z}
dict_df = pd.DataFrame({key:pd.Series(value) for key, value in
mydict.items()})
这是我的AppComponent代码
error: [dagger.android.AndroidInjector.inject(T)] android.app.Application cannot be provided without an @Inject constructor or from an @Provides-annotated method.
android.app.Application is injected at
com.nwrn.pfdy.DI.Module.AppModule.provideContext(application)
android.content.Context is injected at
com.nwrn.pfdy.DI.Module.AppModule.provideAppPreferencesHelper(context)
com.nwrn.pfdy.Data.Prefs.AppPreferencesHelper is injected at
com.nwrn.pfdy.DI.Module.NetworkModule.provideAddCookiesInterceptor(appPreferencesHelper)
com.nwrn.pfdy.Network.Cookies.AddCookiesInterceptor is injected at
com.nwrn.pfdy.DI.Module.NetworkModule.provideRetrofitService(addCookiesInterceptor, …)
com.nwrn.pfdy.Network.Retrofit.RetrofitService is injected at
com.nwrn.pfdy.View.Login.LoginActivityModule.provideLoginViewModel(retrofitService)
com.nwrn.pfdy.View.Login.LoginViewModel is injected at
com.nwrn.pfdy.View.Login.LoginActivity.mLoginViewModel
com.nwrn.pfdy.View.Login.LoginActivity is injected at
dagger.android.AndroidInjector.inject(arg0)
下面是我的模块代码
@Singleton
@Component(modules = {AndroidInjectionModule.class, AppModule.class, ActivityModule.class, NetworkModule.class})
public interface AppComponent {
void inject(BaseApplication baseApplication);
@Component.Builder
interface Builder{
@BindsInstance
Builder application(BaseApplication baseApplication);
AppComponent build();
}
}
这是另一个模块
@Module
public class AppModule {
@Provides
@Singleton
Context provideContext(Application application) {
return application;
}
@Provides
@Singleton
AppPreferencesHelper provideAppPreferencesHelper(Context context) {
return new AppPreferencesHelper(context);
}
}
下面的代码是关于注入类的
@Module
public class NetworkModule {
@Provides
AddCookiesInterceptor provideAddCookiesInterceptor(AppPreferencesHelper appPreferencesHelper) {
return new AddCookiesInterceptor(appPreferencesHelper);
}
@Provides
ReceivedCookiesInterceptor provideReceivedCookiesInterceptor(AppPreferencesHelper appPreferencesHelper) {
return new ReceivedCookiesInterceptor(appPreferencesHelper);
}
@Singleton
@Provides
RetrofitService provideRetrofitService(AddCookiesInterceptor addCookiesInterceptor, ReceivedCookiesInterceptor receivedCookiesInterceptor) {
return new RetrofitClient(addCookiesInterceptor, receivedCookiesInterceptor).getService();
}
}
}
public class RetrofitClient {
private RetrofitService service;
private static final String baseURL = "http://192.168.0.33:8070/";
public RetrofitClient(AddCookiesInterceptor addCookiesInterceptor, ReceivedCookiesInterceptor receivedCookiesInterceptor) {
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
client.interceptors().add(addCookiesInterceptor);
client.interceptors().add(receivedCookiesInterceptor);
Retrofit retrofit = new Retrofit.Builder().baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
service = retrofit.create(RetrofitService.class);
}
public RetrofitService getService() {
return service;
}
}
public class AddCookiesInterceptor implements Interceptor {
private AppPreferencesHelper mAppPreferencesHelper;
public AddCookiesInterceptor(AppPreferencesHelper appPreferencesHelper){
this.mAppPreferencesHelper = appPreferencesHelper;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
Set<String> preferences = mAppPreferencesHelper.getCookie();
for (String cookie : preferences) {
builder.addHeader("Cookie", cookie);
}
// Web,Android,iOS 구분을 위해 User-Agent 세팅
builder.removeHeader("User-Agent").addHeader("User-Agent", "Android");
return chain.proceed(builder.build());
}
}
public class ReceivedCookiesInterceptor implements Interceptor {
private AppPreferencesHelper mAppPreferencesHelper;
public ReceivedCookiesInterceptor(AppPreferencesHelper appPreferencesHelper)
{
this.mAppPreferencesHelper = appPreferencesHelper;
}
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
HashSet<String> cookies = new HashSet<>(originalResponse.headers("Set-Cookie"));
// Preference에 cookies를 넣어주는 작업을 수행
mAppPreferencesHelper.setCookie(cookies);
}
return originalResponse;
}
注入流是这样的 Rerofit2 => AddCookiesInterceptor,ReceivedCookiesInterceptr => AppPreferencesHelper =>上下文
通过测试得到的关于此错误的信息是该错误是由AppModule中的ProvideContext引起的。 如果我更改没有这样的参数的AppPreferencesHelper构造函数(不提供ProvideContext),则不会出错
public class AppPreferencesHelper implements PreferencesHelper {
private static final String PREF_KEY_COOKIE_NAME = "PREF_KEY_COOKIE_NAME";
private final SharedPreferences mPrefs;
public AppPreferencesHelper(Context context) {
mPrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}
@Override
public Set<String> getCookie() {
return mPrefs.getStringSet(PREF_KEY_COOKIE_NAME, new HashSet<String>());
}
@Override
public void setCookie(HashSet<String> cookie) {
mPrefs.edit().putStringSet(PREF_KEY_COOKIE_NAME, cookie);
}
}
public interface PreferencesHelper {
Set<String> getCookie();
void setCookie(HashSet<String> cookie);
}
和
public AppConstructor(){
}
非常感谢您的任何建议
答案 0 :(得分:0)
已编辑
似乎您需要通过Application
方法将BaseApplication
更改为provideContext
。
希望这会有所帮助!