我是Roboguice的新手,請大家幫忙。我有一個應用程序MyApplication
,其中onCreate
方法我初始化一些數據。另外我有一個POJO
buisiness邏輯,我想用我的MainActivity
(請參閱下面的代碼片段)。我需要將MyApplication
注入到POJO
以獲取我在應用程序的onCreate
中初始化的數據,但此代碼在onCreate
之前調用,並且我有NullPointerException
。Roboguice 2.0向POJO注入應用程序
public class MyApplication extends Application {
private Properties applicationProperties;
@Override
public void onCreate() {
super.onCreate();
applicationProperties = loadApplicationProperties(APPLICATION_PROPERTIES_ASSET);
}
@SuppressWarnings("unchecked")
public String getProperty(String key) {
return applicationProperties.getProperty(key);
}
}
@Singleton
public class POJO {
@Inject
private MyApplication application;
@Inject
public void init() {
// NPE here, because application onCreate not called at this moment
serverURL = application.getProperty(Constants.SERVER_URL);
}
}
public class MainActivity extends RoboActivity {
@Inject
private POJO myPOJO;
}
你是否設法最終解決它? –
還沒有,我只是讓我的應用程序單身,並通過MyApplication.getInstance()POJO中獲取實例,而不注入 –