- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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/
Dagger 和 Dagger 2.0 有什么区别,为什么 Google 决定 fork 现有项目? 最佳答案 来自 Dagger 2 presentation 的一些引述 Dagger 1 的问题:
我想制作一个包含一个主要 Activity 和多个 fragment 的简单项目。在这里,我在一项 Activity 中有两个 fragment ,我想将演示者注入(inject)登录 fragmen
我是 dagger 的新手,我正在寻找如何在 dagger-2.x 中实现 spring 配置文件等功能。我想为我的开发环境和生产环境使用不同的 bean,但我使用的是带有 Java 的 Dagger
假设我有两个服务 AService 和 BService,它们都需要一个 API key 。 所以在我的模块中,我不能做类似的事情: @Provides @Singleton @A @ApiKey S
我对 Dagger 很陌生——我什至不知道它是否适用于我的应用程序 我有一个搜索页面,它返回有关给定名人的最新消息。 我写了一个测试来验证当我们搜索一个受欢迎的名人时结果会出现在页面上。 page有一
我最近将 dagger 2.8 更新为 2.9 dagger。和最新版本的文档已添加如下: -添加@BindsInstance组件构建器可以轻松绑定(bind)在图之外构建的实例。 -制作人:已添加
Dagger documentation页面说: To get the most out of compile-time validation, create a module that includ
我可以知道之间的区别吗? @Singleton VS 静态 在dagger2中提供? @Provides static User currentUser(AuthManager authManager
我有一个功能,我需要为不同的用户提供不同的房间数据库。 我正在使用 Dagger 2 创建房间数据库。我的应用程序组件创建一个房间数据库。当用户切换到另一个用户时,我想为此创建新的房间数据库,我需要创
我是 Dagger 2 的新手,正在尝试 IntelliJ 中的 Dagger 2 Coffee Example,它似乎不会生成 DaggerCoffeeApp_Coffee(它应该生成它),即使我密
我正在尝试在 Android 上使用 Dagger 2。我以前让它工作,我有一个 appModule 将依赖项注入(inject)应用程序中的特定类。我的问题是我收到错误 Error:(14, 55)
我是 kotlin 和 Dagger 的新手。我有一个小问题,我不知道如何解决,也没有找到解决方案。 这就是我所拥有的: @Module class AppModule (app: Applicati
我正在尝试使用 Dagger 建立一个项目。现在,当我构建时,没有为组件生成任何 Dagger* 类,并且出现以下错误: Error:(10, 8) error: [com.redditapp.dag
在 Dagger 中,我有时会看到有些组件只扩展一个接口(interface),而其他组件则使用 dependencies。 例如我们有一个基础组件: @Singleton @Component(mo
有没有办法告诉 Dagger 2 如何提供一些东西,但不允许它被注入(inject)? 假设我要注入(inject)一个 Qux .一个 Qux需要 Foo和 Bar : @Provides @Sin
( x-post from /r/androiddev ) 我只想在序言中说这不是一篇“哪个更好”的帖子;这严格来说是一个关于如何使用 Dagger 构建某些东西的问题(以及如何在 Kodein 中构
https://developer.android.com/topic/libraries/architecture/ 在Android架构蓝图中,为什么dagger是基于mvp架构而不是MVVM架构
我第一次开始同时使用 Kotlin 和 Dagger 2。我假设一切都与 Java 中的相同,但显然不完全相同。 Dagger 不会为我生成 Dagger* 文件。这是我的代码: 组件: @PerAc
我正在尝试将 Dagger 2 与 eclipse 集成,我使用的库如下: dagger-2.0.jar dagger-compiler-2.0.jar guava-13.0.1.jar javawr
我想练习这个Dagger 2 Vehicle Motor例子。 除了我的 gradel.build 之外,我所做的一切都与该教程中的一样: compile 'com.google.dagger:dag
我是一名优秀的程序员,十分优秀!