gpt4 book ai didi

android - 方形/ Dagger 。安卓。 java.lang.StackOverflowError 错误

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:28 26 4
gpt4 key购买 nike

尝试为 Android 使用 Dagger 依赖注入(inject)器。这是扩展的应用程序类:

public class MyApplication extends Application {

private ObjectGraph objectGraph;

protected List<Object> getModules() {
List<Object> objectArrayList = new ArrayList<Object>();

objectArrayList.add(new AndroidModule(this));
objectArrayList.add(new DeviceModule());

return objectArrayList;
}

@Override
public void onCreate() {
super.onCreate();

objectGraph = ObjectGraph.create(getModules().toArray());
}

public void inject(Object object) {
objectGraph.inject(object);
}
}

这是我的模块:

安卓模块

@Module(library = true)
public class AndroidModule {
private final MyApplication myApplication;

public AndroidModule(MyApplication myApplication) {
this.myApplication = myApplication;
}

@Provides
@Singleton
@ForApplication
public Context provideApplicationContext() {
return myApplication.getApplicationContext();
}

@Provides
@Singleton
public LocationManager provideLocationManager() {
return (LocationManager)myApplication.getSystemService(Context.LOCATION_SERVICE);
}

@Provides
public NetworkInfo provideNetworkInfo() {
ConnectivityManager connectivityManager = (ConnectivityManager)myApplication.getSystemService(Context.CONNECTIVITY_SERVICE);
return connectivityManager.getActiveNetworkInfo();
}
}

设备模块

 @Module(injects = MainActivity.class, complete = false, library = true)
public class DeviceModule {

@Provides
public SystemValidator provideSystemValidator() {
return new AndroidSystemValidator();
}

@Provides
public Device provideDevice(SystemValidator systemValidator) {
return new Device(systemValidator);
}
}

设备类

public class Device {
private final SystemValidator systemValidator;

@Inject
public Device(SystemValidator systemValidator) {
this.systemValidator = systemValidator;
}

public void validate() {
boolean isGPSTurnedOn = systemValidator.isGPS();
boolean isNetworkActive = systemValidator.isNetwork();
}
}

SystemValidator 类

public class AndroidSystemValidator implements SystemValidator {
@Inject
LocationManager locationManager;

@Inject
NetworkInfo networkInfo;

@Override
public boolean isNetwork() {
return (null != networkInfo && (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE));
}

@Override
public boolean isGPS() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
}

这段成功编译,但在运行时我得到 java.lang.StackOverflowError试图注入(inject) MainActivity对象到 ObjectGraph

java.lang.StackOverflowError
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:77)
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:113)
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:77)
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:113)
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:77)
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:113)

已编辑:

代码已被可行的解决方案更改,但即使 LocationManagerNetworkInfo对象标记为 @Inject , 是 null。试图告诉AndroidModule通过添加 injects = AndroidSystemValidator.class 放置这些对象的位置但没有成功。如何在不创建参数化构造函数的情况下注入(inject)这些对象?

最佳答案

你有一个循环依赖

@Provides
public Device provideDevice(Device device) {
return device;
}

当 Dagger 尝试获取 Device 实例以用作 provideDevice 方法的参数时,它会一次又一次地为其调用 provideDevice StackOverflowError 发生。但这只是我的假设。

关于android - 方形/ Dagger 。安卓。 java.lang.StackOverflowError 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22963687/

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