문제

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
도움이 되었습니까?

해결책

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. Not entirely true! See edit!

EDIT

As superjos noted in the comments, you do need ID's when working with relative layouts. Check out the docs on RelativeLayout.LayoutParams.addRule(int,int) for when you need ID's.

END EDIT

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.

다른 팁

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

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

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

You can try it again and don't attend a call or any process while your app is loading. Wait till it sets.

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.

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top