【问题标题】:Android Dagger 2 is asking for @Provides while it's thereAndroid Dagger 2 在那里时要求@Provides
【发布时间】:2016-07-26 08:31:35
【问题描述】:

我正在尝试使用 dagger 2 在我的项目中实施改造,它给了我以下错误:

Error:(22, 10) error: com.toranj.tyke.restApi.LotteryApiInterface cannot be provided without an @Provides- or @Produces-annotated method.
com.toranj.tyke.restApi.LotteryApiInterface is injected at
com.toranj.tyke.ui.MainActivity.LotteryApiInterface
com.toranj.tyke.ui.MainActivity is injected at
com.toranj.tyke.dagger.components.NetworkComponent.inject(activity)

这是我的 LotteryApiInterface

public interface LotteryApiInterface {
    @GET("/lottery/search")
    Call<List<Lottery>> getByCriteria(@Query("q") String query);

    @GET("lottery/details")
    Call<Lottery> getById(@Query("Id") String id);
}

这里是 LotteryComponent

@PerActivity
@Component(dependencies = NetworkComponent.class, modules = LotteryModule.class)
public interface LotteryComponent {
    void inject(MainActivity activity);

}

我的 LotteryModule 就到这里了:

@Module
public class LotteryModule {

    @Provides
    @PerActivity
    public LotteryApiInterface providesLotteryApiInterface(Retrofit retrofit) {
        return retrofit.create(LotteryApiInterface.class);
    }
}

当我 Clean > Rebuild 项目时,即使我的 LotteryModule 具有对象的 @Provides 注释,它也会给我错误。

我可能在这里遗漏了一些东西。任何帮助表示赞赏。

编辑:改造是在我的应用程序类中创建的,如下所示:

public class TykeApp extends Application {

    private NetworkComponent networkComponent;

    @Override
    public void onCreate() {
        super.onCreate();

        networkComponent = DaggerNetworkComponent.builder()
                .appModule(new AppModule(this))
                .networkModule(new     NetworkModule("http://192.168.0.3:8080"))
                .build();
    }

    public NetworkComponent getNetworkComponent() {
        return networkComponent;
    }
}

网络组件

@Singleton
@Component(modules = { AppModule.class, NetworkModule.class })
public interface NetworkComponent {
    void inject(MainActivity activity);

    Retrofit retrofit();
    OkHttpClient okHttpClient();
    SharedPreferences sharedPreferences();
}

网络模块:

@Module
public class NetworkModule {

    String baseUrl;

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

    @Provides
    SharedPreferences provideSharedPreferences(Application application) {
        return PreferenceManager.getDefaultSharedPreferences(application);
    }

    @Provides
    OkHttpClient provideOkhttpClient() {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        return client.build();
    }

    @Provides
    Retrofit provideRetrofit(OkHttpClient httpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .client(httpClient)
                .build();
    }
}

这是 MainActivity:

public class MainActivity extends BaseActivity {

    @Inject
    Retrofit retrofit;

    @Inject
    LotteryApiInterface LotteryApiInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((TykeApp)getApplication()).getNetworkComponent().inject(this);

        LotteryComponent lotteryComponent = DaggerLotteryComponent.builder()
                .lotteryModule(new LotteryModule())
                .build();
        lotteryComponent.inject(this);
    }
}

当我使用时

@Inejct 
LotteryApiInterface LotteryApiInterface; 

它抛出给定的错误消息。

【问题讨论】:

  • 你在哪里创建 Retrofit 本身?
  • Retrofit 是在 Application 中创建的,它是单例的,它工作得很好。我会更新问题
  • Retrofit 传递给该方法public LotteryApiInterface providesLotteryApiInterface(Retrofit retrofit) { 应该用匕首创建
  • 您的实际错误也与NetworkComponentMainActivity 有关
  • @EpicPandaForce 我知道。彩票组件无法正常工作,并且错误消息与改造界面有关。 \@Provides 已为 LotteryModule 添加,但它仍在要求它

标签: android retrofit retrofit2 dagger-2 dagger


【解决方案1】:

当您在 Dagger2 中使用 字段注入 时,您使用 component.inject(this); 的组件需要能够提供每个依赖项你用@Inject标记的。

这意味着在你的情况下,你不应该有

    ((TykeApp)getApplication()).getNetworkComponent().inject(this);

相反,你应该有

    LotteryComponent lotteryComponent = DaggerLotteryComponent.builder()
            .networkComponent(((TykeApp)getApplication()).getNetworkComponent())
            .lotteryModule(new LotteryModule())
            .build();
    lotteryComponent.inject(this);

但是值得注意的是你的网络模块应该是

@Module
public class NetworkModule {

    String baseUrl;

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

    @Provides
    SharedPreferences provideSharedPreferences(Application application) {
        return PreferenceManager.getDefaultSharedPreferences(application);
    }

    @Provides
    @Singleton
    OkHttpClient provideOkhttpClient() {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        return client.build();
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient httpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .client(httpClient)
                .build();
    }
}

你的 NetworkComponent 应该是

@Singleton
@Component(modules = { AppModule.class, NetworkModule.class })
public interface NetworkComponent {
    Retrofit retrofit();
    OkHttpClient okHttpClient();
    SharedPreferences sharedPreferences();
}

【讨论】:

  • 哇...太棒了,它奏效了。谢谢您的帮助。我明白你的意思了。干杯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-12
  • 2017-11-05
  • 1970-01-01
相关资源
最近更新 更多