Dagger Module must be set(必须设置匕首模块)
本文介绍了必须设置匕首模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试添加Dagger2。但是,我不能确定这个编译错误:MainActivityModule must be set
,它发生在我的MainActivity
中。如果有人试图解释这个错误的话。我真的很高兴。
我的主要活动
public class MainActivity extends BaseActivity {
@Inject
JsonMethods mJsonMethods;
...
in OnCreate
Weather.getComponent().injectMainActivity(this);
我的天气类
public class Weather extends Application {
private static WeatherComponent sWeatherComponent;
@Override
public void onCreate() {
super.onCreate();
sWeatherComponent = DaggerWeatherComponent.builder()
.contextModule(new ContextModule(this))
.build();
}
public static WeatherComponent getComponent() {
return sWeatherComponent;
}
}
天气组件
@Component(modules = MainActivityModule.class)
public interface WeatherComponent {
void injectMainActivity(MainActivity mainActivity);
}
主活动模块
@Module(includes = NetworkModule.class)
public class MainActivityModule {
private final MainActivity mainActivity;
public MainActivityModule(MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
@Provides
@WeatherApplicationScope
public JsonMethods provideJsonMethods(Retrofit retrofit) {
return new JsonMethods(mainActivity, retrofit);
}
网络模块返回Retrofit
、Gson
、OkHttpClient
错误MainActivityModule must be set
出现在此代码中:
sWeatherComponent = DaggerWeatherComponent.builder()
.contextModule(new ContextModule(this))
.build();
推荐答案
您必须像创建ContextModule
实例一样创建MainActivityModule
实例。您的MainActivityModule
没有任何默认构造函数,因此Dagger
无法创建MainActivityModule
的实例。因此创建一个MainActivityModule
实例并将其设置为Dagger
图。
sWeatherComponent = DaggerWeatherComponent.builder()
.contextModule(new ContextModule(this))
.mainActivityModule(new MainActivityModule(....))
.build();
如果模块没有任何用户定义的构造函数,则Dagger
将在需要时通过默认构造函数自动创建一个实例。
这篇关于必须设置匕首模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:必须设置匕首模块


基础教程推荐
猜你喜欢
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01