【发布时间】: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) {应该用匕首创建 -
您的实际错误也与
NetworkComponent或MainActivity有关 -
@EpicPandaForce 我知道。彩票组件无法正常工作,并且错误消息与改造界面有关。 \@Provides 已为 LotteryModule 添加,但它仍在要求它
标签: android retrofit retrofit2 dagger-2 dagger