Java 匕首2中的依赖关系

Java 匕首2中的依赖关系,java,android,dependency-injection,dagger-2,dagger,Java,Android,Dependency Injection,Dagger 2,Dagger,我遇到了这个例子。 创建项目后,我遇到了这个错误 Error:(18, 10) error: android.app.Application cannot be provided without an @Inject constructor or from an @Provides-annotated method. android.app.Application is injected at com.app.series.dagger.modules.NetworkModule.provide

我遇到了这个例子。 创建项目后,我遇到了这个错误

Error:(18, 10) error: android.app.Application cannot be provided without an @Inject constructor or from an @Provides-annotated method.
android.app.Application is injected at
com.app.series.dagger.modules.NetworkModule.provideHttpCache(application)
okhttp3.Cache is injected at
com.app.series.dagger.modules.NetworkModule.provideOkhttpClient(cache)
okhttp3.OkHttpClient is injected at
com.app.series.dagger.modules.NetworkModule.provideRetrofit(…, okHttpClient)
retrofit2.Retrofit is injected at
com.app.series.activities.MainActivity.retrofit
com.app.series.activities.MainActivity is injected at
com.app.series.dagger.components.NetComponent.inject(activity)
此外,我有一个与DaggerNetComponent相关的错误,无法解决,但通过在线阅读,我认为这可能是因为第一个错误

据我所知,这似乎与传递活动有关,而不是应用程序,反之亦然。我花了好几个小时试图理解这个问题是从哪里来的,但我看不出来

这是到目前为止我的代码

应用模块

@Module
public class AppModule
{
    SeriesApplication mApplication;

    public AppModule(SeriesApplication mApplication) {
        this.mApplication = mApplication;
    }

    @Provides
    @Singleton
    SeriesApplication provideApplication() {
        return mApplication;
    }
}
系列应用

public class SeriesApplication extends Application
{
    private NetComponent mNetComponent;

    private static SeriesApplication instance;
    public static SeriesApplication getInstance() {
        return instance;
    }
    public static void setInstance(SeriesApplication instance) {
        SeriesApplication.instance = instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setInstance(this);
        mNetComponent = DaggerNetComponent.builder()
                .appModule(new AppModule(this))
                .netModule(new NetworkModule("http://www.jsonplaceholder.typicode.com/"))
                .build();
    }

    public NetComponent getNetComponent() {
        return mNetComponent;
    }
}
网络组件

@Singleton
@Component(modules = {AppModule.class, NetworkModule.class})
public interface NetComponent {
    void inject(MainActivity activity);
}
网络模块

@Module
public class NetworkModule {
    String mBaseUrl;

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

    @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) {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.cache(cache);
        return client.build();
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
    }
}
主要活动

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((SeriesApplication) getApplication()).getNetComponent().inject(this);



        //Create a retrofit call object
        Call<List<Series>> posts = retrofit.create(Restapi.class).getPosts();

        //Enque the call
        posts.enqueue(new Callback<List<Series>>() {
            @Override
            public void onResponse(Call<List<Series>> call, Response<List<Series>> response) {
                //Set the response to the textview
//                textView.setText(response.body().get(0).getBody());

            }

            @Override
            public void onFailure(Call<List<Series>> call, Throwable t) {
                //Set the error to the textview
//                textView.setText(t.toString());
            }
        });

非常感谢您在这一点上提供的帮助,感谢您在
NetworkModule
中提供的
Application
,而不是
系列应用程序


使用以下代码:


    @Provides
    @Singleton
    Cache provideHttpCache(Application application) {
        ...
    }
您告诉dagger,“每当我要求您提供
缓存
,请使用
应用程序
,以构建
缓存

但您还没有指定dagger应该如何获取
应用程序的实例。
以下提供程序方法返回完全不同的对象:


    @Provides
    @Singleton
    SeriesApplication provideApplication() {
        return mApplication;
    }
它返回一个
系列应用程序的实例。
相反,将返回类型更改为
Application


    @Provides
    @Singleton
    Application provideApplication() {
        return mApplication;
    }
现在匕首不会被混淆了


    @Provides
    @Singleton
    Cache provideHttpCache(Application application) {
        ...
    }

    @Provides
    @Singleton
    SeriesApplication provideApplication() {
        return mApplication;
    }

    @Provides
    @Singleton
    Application provideApplication() {
        return mApplication;
    }