views:

187

answers:

6

After I either receive a phone call or make one, (and other undocumented interruptions) my application gets a NullPointerException when resuming my activity. Can any explain to me where it is and/or how to fix it? When my activity resumes, it is calling onCreate it seems, and it is trying to execute something that is null after Resuming. How do I prevent onCreate() from being called?

My activity seems to terminate when I press the call button, because when I try to debug this error, the debugger disconnects.

EDIT:

So, how do I handle process is killed -> onCreate() ? I have activities A -> B -> C -> D, and I press back all the way to A, there is no problem. But If I start another program, or another program comes to the foreground, D crashes, then C crashes, then B crashes, then A crashes!

EDIT:

I solved B,C,D crashing. It was because the class where I stored static variables was destroyed to free up resources, and my activities were getting null variables.

But when I get back to A, I get a classCastException:

08-13 16:52:10.456: ERROR/AndroidRuntime(6048): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bookcessed.booksearch/com.bookcessed.booksearch.activities.ChooseProviderActivity}: java.lang.ClassCastException: android.view.AbsSavedState$1
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.os.Looper.loop(Looper.java:123)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at java.lang.reflect.Method.invoke(Method.java:521)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at dalvik.system.NativeStart.main(Native Method)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048): Caused by: java.lang.ClassCastException: android.view.AbsSavedState$1
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.widget.ProgressBar.onRestoreInstanceState(ProgressBar.java:944)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.view.View.dispatchRestoreInstanceState(View.java:6138)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1209)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1209)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.view.View.restoreHierarchyState(View.java:6117)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1466)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.app.Activity.onRestoreInstanceState(Activity.java:843)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.app.Activity.performRestoreInstanceState(Activity.java:815)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641)
08-13 16:52:10.456: ERROR/AndroidRuntime(6048):     ... 11 more

Here is my onCreate():

super.onCreate(savedInstanceState);
        tempLayout = new RelativeLayout(ChooseProviderActivity.this);
        ProgressBar tempProgress = new ProgressBar(ChooseProviderActivity.this);
        tempProgress.setIndeterminate(true);
        tempProgress.setId(1); //I suspect this is the problem
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        tempLayout.addView(tempProgress, lp);
        setContentView(tempLayout);

This is where I think the problem lies:

tempProgress.setId(1); //I suspect this is the problem
+1  A: 

Check out this image to see how and when the os will call your app. alt text

monoceres
So, how do I handleprocess is killed -> onCreate() ?
Aymon Fournier
This might help: http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state
Asahi
A: 

Why don't you try and override all the basic functions which are used during the activity life cycle. use the adb logcat figure out with log.v where your error is occuring. Put one log after every line in the function in which the app crashes.

Also what are you calling from? are you calling from one emulator to the other? I have not had a problem with the log cat existing in such an instance.

Shouvik
+1  A: 

Your app gets terminated for some reason, that's why onCreate is being called. You should save/resume application state to be able to handle this correctly. Have a look at this and developers reference

Asahi
A: 

Maybe your app uses too much memory so Android OS kills it in order to get enough memory to run another activity.

Tiago Costa
A: 

I was using a class with static variables and calling .getStoredStaticVariable() and .saveStoredStaticVariable() and when I would try to retrieve it, it would be null, probably because the class was being destroyed to free up resources.

EDIT: That was only partially the solution. The solution to the ClassCastException() was changing

tempProgress.setId(1);

to a number unlikely to be used by the system:

tempProgress.setId(6346346);
Aymon Fournier
+1  A: 

Just for other people's clarification, you do not need to assign ID's to Views that you instantiate in code. So you're code should work with just the

ProgressBar tempProgress = new ProgressBar(ChooseProviderActivity.this);
tempProgress.setIndeterminate(true);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
tempLayout.addView(tempProgress, lp);
setContentView(tempLayout);

Id's are only mainly when setting ID's in XML files, you assign it a string ID and the R.java is then compiled, and assigns each resource an ID number.

Like this...

LinearLayout ll = new LinearLayout(SomeClass.this);
ll.setOrientation(VERTICAL);
TextView tv = new TextView(SomeClass.this);
EditText et = new EditText(SomeClass.this);
ll.add(tv);
ll.add(et);

This will have a textView above an EditText view. In the other case... doing

ll.add(et);
ll.add(tv);

will place the EditText ABOVE the TextView. ID's have absolutely nothing to do with how they are laid out on the screen.

DavidAndroidDev
I thought you had to use it if you wanted to specify how objects should be layed out with respect to one another
Aymon Fournier
No, how they are laid out is dependant on what LayoutParameters you set on a view and WHEN you add it to a viewgroup. I edited my answer with some example code...
DavidAndroidDev