接到电话后应用程序崩溃

发布于 2024-09-14 03:09:31 字数 4360 浏览 7 评论 0原文

在我接到电话或拨打电话(以及其他未记录的中断)后,我的应用程序在恢复活动时收到 NullPointerException。任何人都可以向我解释它在哪里和/或如何修复它吗?当我的活动恢复时,它似乎正在调用 onCreate,并且它正在尝试执行恢复后为空的内容。如何防止 onCreate() 被调用?

当我按下呼叫按钮时,我的活动似乎终止,因为当我尝试调试此错误时,调试器会断开连接。

编辑:

那么,我该如何处理 进程被杀死-> onCreate() ?我有活动 A -> B-> C-> D、我一路按回A,没有问题。但是如果我启动另一个程序,或者另一个程序进入前台,D 崩溃,然后 C 崩溃,然后 B 崩溃,然后 A 崩溃!

编辑:

我解决了 B、C、D 崩溃问题。这是因为我存储静态变量的类被销毁以释放资源,并且我的活动获取了空变量。

但是当我回到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);

这就是我认为问题所在的地方:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

游魂 2024-09-21 03:09:31

查看此图片,了解操作系统如何以及何时调用您的应用程序。
替代文本

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

花落人断肠 2024-09-21 03:09:31

仅供其他人澄清,您不需要为在代码中实例化的视图分配 ID。因此,您的代码应该只使用 ID

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);

,主要是在 XML 文件中设置 ID 时,为它分配一个字符串 ID,然后编译 R.java,并为每个资源分配一个 ID 号。 不完全正确!请参阅编辑!

编辑

正如评论中所说的那样,在使用相对布局时,您确实需要 ID。当您需要ID时,请查看RelativeLayout.LayoutParams.addRule(int,int)上的文档。

END EDIT

像这样...

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);

这将在 EditText 视图上方有一个 textView。在另一种情况下...
这样做

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

会将 EditText 放置在 TextView 之上。 ID 与它们在屏幕上的布局完全无关。

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.

dawn曙光 2024-09-21 03:09:31

您的应用程序因某种原因终止,这就是调用 onCreate 的原因。您应该保存/恢复应用程序状态才能正确处理此问题。看看这个开发人员参考

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

勿挽旧人 2024-09-21 03:09:31

也许您的应用程序使用了太多内存,因此 Android 操作系统会杀死它以获得足够的内存来运行另一个活动。

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

愁杀 2024-09-21 03:09:31

您可以再试一次,并且在加载应用程序时不要接听电话或任何进程。等到它凝固。

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

十级心震 2024-09-21 03:09:31

为什么不尝试重写活动生命周期中使用的所有基本函数。使用 adb logcat 和 log.v 找出发生错误的位置。在应用程序崩溃的函数中的每一行后面添加一个日志。

还有你是从哪里打来的电话?您是从一个模拟器调用另一个模拟器吗?在这种情况下,我对日志猫没有遇到任何问题。

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.

挽梦忆笙歌 2024-09-21 03:09:31

我使用带有静态变量的类并调用 .getStoredStaticVariable().saveStoredStaticVariable() ,当我尝试检索它时,它将为 null,可能是因为类被销毁以释放资源。

编辑:这只是部分解决方案。 ClassCastException() 的解决方案是更改

tempProgress.setId(1);

为系统不太可能使用的数字:

tempProgress.setId(6346346);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文