receving电话后应用程序崩溃 [英] App crashes after receving phone call

查看:139
本文介绍了receving电话后应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我要么收到电话或做一个,(和其他无证中断)重新开始我的活动时,我的申请得到一个NullPointerException异常。可以在任何解释给我在那里和/或如何解决呢?当我的活动恢复,它调用的onCreate似乎和它正试图执行的东西是恢复后空。我如何prevent的onCreate()被调用?

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

编辑:

那么,我该如何处理 进程被杀死 - >的onCreate()?我有活动的一个 - 乙 - ç - > D和我preSS回一路A,是没有问题的。但是,如果我开始另一个程序,或其他程序来到前台,D崩溃,那么C崩溃,那么B崩溃,那么A崩溃!

编辑:

我解决了B,C,D崩溃。这是因为在那里我储存的静态变量的类被销毁,以释放资源,我的活动越来越空的变量。

但是,当我回到A,我得到一个ClassCastException:

  08-13 16:52:10.456:ERROR / AndroidRuntime(6048):java.lang.RuntimeException的:无法启动的活动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):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.os.Handler.dispatchMessage(Handler.java:99)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.os.Looper.loop(Looper.java:123)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.app.ActivityThread.main(ActivityThread.java:4627)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在java.lang.reflect.Method.invokeNative(本机方法)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在java.lang.reflect.Method.invoke(Method.java:521)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在dalvik.system.NativeStart.main(本机方法)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):java.lang.ClassCastException:产生的原因android.view.AbsSavedState $ 1
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.widget.ProgressBar.onRestoreInstanceState(ProgressBar.java:944)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.view.View.dispatchRestoreInstanceState(View.java:6138)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1209)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1209)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.view.View.restoreHierarchyState(View.java:6117)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1466)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.app.Activity.onRestoreInstanceState(Activity.java:843)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.app.Activity.performRestoreInstanceState(Activity.java:815)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641)
08-13 16:52:10.456:ERROR / AndroidRuntime(6048):11 ...更多
 

下面是我的onCreate():

  super.onCreate(savedInstanceState);
        tempLayout =新RelativeLayout的(ChooseProviderActivity.this);
        进度tempProgress =新进度(ChooseProviderActivity.this);
        tempProgress.setIndeterminate(真正的);
        tempProgress.setId(1); //我怀疑这是问题
        RelativeLayout.LayoutParams LP =新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); //我怀疑这是问题
 

解决方案

只是其他人的澄清,你不需要指定的ID您在code实例化视图。所以你code应该只用

 进度tempProgress =新进度(ChooseProviderActivity.this);
tempProgress.setIndeterminate(真正的);
RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
tempLayout.addView(tempProgress,LP);
的setContentView(tempLayout);
 

ID是唯一的主要设置ID在XML文件时,你为它分配一个字符串ID和R.java然后编译,并分配每个资源的ID号。并非完全如此!见编辑!

修改

由于superjos在评论中指出的,你需要的ID相对布局工作时。检查出的文档上RelativeLayout.LayoutParams.addRule(INT,INT),当你需要ID的。

END修改

就像这个...

 的LinearLayout LL =新的LinearLayout(SomeClass.this);
ll.setOrientation(垂直);
TextView的电视=新的TextView(SomeClass.this);
等的EditText =新的EditText(SomeClass.this);
ll.add(电视);
ll.add(等);
 

这将有上面一个EditText视图一个TextView。在其他情况下... 做

  ll.add(等);
ll.add(电视);
 

将放置的EditText以上的TextView。 ID的绝对无关,与他们是如何布局在屏幕上。

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.

这篇关于receving电话后应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆