我正在使用Android Dagger2,但出现了类似的错误

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)


这是我的AppComponent代码

@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;
}


}

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);

}


注射流量是这样的
Rerofit2 => AddCookiesInterceptor,ReceivedCookiesInterceptr => AppPreferencesHelper =>上下文

我通过测试得到的关于此错误的信息是该错误是由AppModule中的ProvideContext引起的。
如果我更改没有这样的参数的AppPreferencesHelper构造函数(不提供ProvideContext),则不会出错

public AppConstructor(){

}




@Module
public class AppModule {

    @Provides
    @Singleton
    Context provideContext(Application application) {
        return application;
    }

    @Provides
    @Singleton
    AppPreferencesHelper provideAppPreferencesHelper() {
        return new AppPreferencesHelper();
    }

}


非常感谢您的任何建议

最佳答案

已编辑

似乎您需要在Application方法中将BaseApplication更改为provideContext

希望这可以帮助!

关于java - Dagger2-没有@Inject构造函数或@Provides注释方法无法提供,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54106036/

10-12 04:17
查看更多