在我接到电话或拨打电话(和其他未记录的中断)后,我的应用程序在恢复活动时会收到NullPointerException.可以向我解释它在哪里和/或如何解决它?当我的活动恢复时,它似乎正在调用onCreate,它正在尝试执行Resuming后返回null的内容.如何防止调用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.AbsSavedState8-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.access00(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.AbsSavedState8-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
这是我的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
解决方法:
只是为了其他人的澄清,您不需要将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);
ID只是主要在XML文件中设置ID时,为其分配一个字符串ID,然后编译R.java,并为每个资源分配一个ID号.不完全正确!看编辑!
编辑
正如评论中提到的superjos,在使用相对布局时,您确实需要ID.查看relativeLayout.LayoutParams.addRule(int,int)上的文档,了解何时需要ID.
结束编辑
像这样…
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与它们在屏幕上的布局完全无关.
总结以上是内存溢出为你收集整理的android – App收到电话后崩溃了全部内容,希望文章能够帮你解决android – App收到电话后崩溃了所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)