我正在尝试使用匕首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);
}
}
当我清理>重建项目时,即使我的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;
}
}
NetworkComponent:
@Singleton
@Component(modules = { AppModule.class, NetworkModule.class })
public interface NetworkComponent {
void inject(MainActivity activity);
Retrofit retrofit();
OkHttpClient okHttpClient();
SharedPreferences sharedPreferences();
}
NetworkModule:
@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中使用字段注入时,使用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();
}