Race state between Application onCreate and loaded resources?

I have the following application class for my application. When the application starts, I want to get some settings from the settings and start the background service.

public class MyApplication extends Application { public void onCreate() { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); String key = getResources().getString(R.string.prefkey_updateinterval); ... } 

This works fine, but sometimes when I run 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 identifier is from my R.java:

 public static final int prefkey_updateinterval=0x7f0a0004; 

Since this works fine in most cases, I have to assume that there is some kind of race condition between onCreate and downloadable resources?

If so, is it advisable to read resources in the onCreate app?

If so, is there a better place to initialize the service when the application starts?

+8
android
source share
2 answers

Since this works fine in most cases, I have to assume that there is some kind of race condition between onCreate and downloadable resources?

If the same APK file - do not recompile, do not reinstall, etc. - does not cause error generation, it may be some kind of race condition, although it will surprise me.

If the same APK file successively fails, then there are more problems with the variety of the garden than the problem of synchronizing with the rest of the code, and cleaning up the project will clear it.

Is there a better place to initialize the service when the application starts?

IMHO, the number of applications that should โ€œinitialize the service when the application startsโ€ is extremely low, to the extent that I canโ€™t come up with a cuff script where it will be a good plan. This does not mean that you do not have such a scenario, but it is a serious smell of code in my book without explanation.

+6
source share

I noticed that in cases where a lot of resources are used (and generated in R.java), cleaning up the application before starting fixes these problems. Therefore, I would suggest that this is not a race condition, but a problem with the Eclipse or Android SDK with non-refreshing resources. As for the placement of the code, it seems to me as good as any other option.

+1
source share

All Articles