gpt4 book ai didi

Android Dagger 2 在那里时要求@Provides

转载 作者:行者123 更新时间:2023-11-29 19:41:36 26 4
gpt4 key购买 nike

我正在尝试使用 dagger 2 在我的项目中实现 Retrofit,但出现以下错误:

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;

它抛出给定的错误信息。

最佳答案

当你在 Dagger2 中使用 field injection 时,你使用 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();
}

关于Android Dagger 2 在那里时要求@Provides,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38584784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com