アプリには次のアプリケーション クラスがあります。アプリケーションの起動時に、設定からいくつかの設定を取得して、バックグラウンド サービスを開始したいと考えています。
public class MyApplication extends Application {
public void onCreate() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String key = getResources().getString(R.string.prefkey_updateinterval);
...
}
This normally works fine, but occasionally when starting my program from eclipse "Run" I get this error:
10-10 08:25:47.016: E/AndroidRuntime(26402): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f0a0004
10-10 08:25:47.016: E/AndroidRuntime(26402): at android.content.res.Resources.getText(Resources.java:216)
10-10 08:25:47.016: E/AndroidRuntime(26402): at android.content.res.Resources.getString(Resources.java:269)
10-10 08:25:47.016: E/AndroidRuntime(26402): at com.karwosts.MyApp.PortfolioStore.onCreate(PortfolioStore.java:40)
10-10 08:25:47.016: E/AndroidRuntime(26402): at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
10-10 08:25:47.016: E/AndroidRuntime(26402): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3395)
This id is from my R.java:
public static final int prefkey_updateinterval=0x7f0a0004;
Since this works fine most of the time, I have to assume that there is some kind of race condition between onCreate and the resources being loaded?
If that's the case, is it recommended not to read resources in Application onCreate?
If so, is there a better place to initialize a service when application launches?