• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Odd error due to task switching

Amanoo

Newbie
Mar 4, 2012
19
0
Hello. I am working on my own app. It appears that when I switch to other tasks after opening and navigating through my app, and switch back to my app later, it crashes. When not task switching, it seems perfectly stable, but task switching tends to make this crash. This does not always happen. If I only quickly switch to email and back, it works perfectly, but if I run several games, and then switch back, the problem occurs. I guess it is something in Android's memory manager that my app doesn't work properly with, but I can't figure out what exactly the problem is.

This is my log:


07-29 22:10:22.450: W/dalvikvm(19946): threadid=1: thread exiting with uncaught exception (group=0x40c4d1f8)
07-29 22:10:22.490: E/AndroidRuntime(19946): FATAL EXCEPTION: main
07-29 22:10:22.490: E/AndroidRuntime(19946): java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.psssoftware.mannashaker/nl.psssoftware.mannashaker.VerseActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.app.ActivityThread.access$600(ActivityThread.java:128)
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.os.Looper.loop(Looper.java:137)
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-29 22:10:22.490: E/AndroidRuntime(19946): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 22:10:22.490: E/AndroidRuntime(19946): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 22:10:22.490: E/AndroidRuntime(19946): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-29 22:10:22.490: E/AndroidRuntime(19946): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-29 22:10:22.490: E/AndroidRuntime(19946): at dalvik.system.NativeStart.main(Native Method)
07-29 22:10:22.490: E/AndroidRuntime(19946): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.content.res.Resources.getText(Resources.java:260)
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.widget.TextView.setText(TextView.java:3669)
07-29 22:10:22.490: E/AndroidRuntime(19946): at nl.psssoftware.mannashaker.VerseActivity.displayVerse(VerseActivity.java:46)
07-29 22:10:22.490: E/AndroidRuntime(19946): at nl.psssoftware.mannashaker.VerseActivity.onCreate(VerseActivity.java:24)
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.app.Activity.performCreate(Activity.java:4533)
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-29 22:10:22.490: E/AndroidRuntime(19946): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-29 22:10:22.490: E/AndroidRuntime(19946): ... 11 more
 
I do call the superclass in my onCreate() functions. I do not have an onPause implemented. Doesn't that mean super.onPause() would be used automatically, since I didn't override it?
Or should I include a seperate block:
protected void onPause() {
super.onPause();
}

In my VerseActivity, which is mentioned in the log, I do have a part where some objects that were created earlier in a different Activity are read. These objects need to be able to be read from every Activity, which is why I put them in a seperate class that only contains static variables and objects. This allows me to manipulate the objects and variable from every view, as is required (it's static, every class knows about them even without defining their existence). When running normally (not task switching) this works exactly as I had in mind. Could that be a problem to my task switching business?
 
Upvote 0
That's right, if you don't implement onPause then the super class function gets called instead.

It is crashing on line 46 of VerseActivity.java, that's where you should be looking for the bug:

Code:
07-29 22:10:22.490: E/AndroidRuntime(19946): 	at nl.psssoftware.mannashaker.VerseActivity.displayVe  rse(VerseActivity.java:46)

Any static data in your application will not persist once the process has been destroyed. You will need to implement an onPause function and stash the data somewhere, then restore it when onCreate is called. Assuming you just have a few strings and integers it is easy to put them into a Bundle object.
 
Upvote 0
Among them is a whole SQLiteDatabase. The other data consists mostly strings and even integers. Can I put a SQLiteDatabase in a Bundle?

It is odd that it crashes on line 46. Here are lines 40 through 46:
Code:
		String[] columns = { "ZVERS" };
		Cursor cursor = SharedMemory.databaseHelper.getBibleDatabase().query(
				"ZDBO_VERZEN", columns,
				"ZBNUMBER=" + SharedMemory.currentBook+" AND ZCNUMBER=" + SharedMemory.currentChapter+" AND ZVNUMBER=" + SharedMemory.currentVerse, null, null,
				null, null);
		cursor.moveToFirst();
		bibletext.setText(cursor.getString(0));
bibletext is defined as a TextView object and is created through the normal findViewById() means in onCreate(). SharedMemory is the class with the statics. How would you suggest me to save this data?
 
Upvote 0
Are you sure you haven't changed your code since you generated the callstack? The exception (android.content.res.Resources$NotFoundException) in the callstack suggests you were trying to get the string from your Resource file rather than from the cursor.

There is no need to put the SQL Database into the bundle. It is already saved to disk so the data in it will persist beyond the point when your process is killed. You can just open the database file again in your onCreate().
 
Upvote 0
I did not change the code. I keep on testing each little change like a madman, so my phone always contains the latest version of the code.
Sometimes I really don't think of the obvious... I could just as well save which of several database files I need to open (each database file has the same structure but is in a different language) as a String in the Bundle, which would leave my SharedMemory with only Strings and integers that need to be saved as well. Thanks for pointing it out for me.
Now I need to find out how to save some Strings and integers to the Bundle. Those would contain enough info to recreate the whole process. I suppose I only need to do this in onPause(), right? onStop() appears to be what happens if the app is meant to be shut down.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones