应用程序在接到电话或此类中断后崩溃
在我接到电话或拨打电话(以及其他未记录的中断)后,我的应用程序在恢复活动时收到 NullPointerException 。任何人都可以向我解释它在哪里和/或如何修复它吗?
我编写的代码如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displayActivity);
new performBackgroundTask().execute();
}
@Override
public void onDestroy()
{
listview1.setAdapter(null);
super.onDestroy();
}
private class performBackgroundTask extends AsyncTask <Void, Void, Void>
{
private ProgressDialog Dialog = new ProgressDialog(displayActivity.this);
protected void onPreExecute()
{
Dialog.setMessage(getString(R.string.dialog_wait_message));
Dialog.show();
}
protected void onPostExecute(Void unused)
{
Dialog.dismiss();
// displaying all the fetched data
}
@Override
protected Void doInBackground(Void... params)
{
// fetching data from web using HTTPGet method
return null;
}
}
Logcat 输出为:
java.lang.RuntimeException: Unable to destroy Activity {myApplication/MyApplication.displayActivity}: java.lang.NullPointerException 在 android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655) 在 android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673) 在 android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3789) 在 android.app.ActivityThread.access$2400(ActivityThread.java:125) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:123) 在 android.app.ActivityThread.main(ActivityThread.java:4627) 在 java.lang.reflect.Method.invokeNative(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:521) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 在dalvik.system.NativeStart.main(本机方法) 引起原因:java.lang.NullPointerException 在 MyApplication.displayActivity.onDestroy(displayActivity.java:74) 在 android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)
现在,请帮我解决这个问题,我如何处理收到的电话或短信或此类中断?
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?
I have written the code as shown below:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displayActivity);
new performBackgroundTask().execute();
}
@Override
public void onDestroy()
{
listview1.setAdapter(null);
super.onDestroy();
}
private class performBackgroundTask extends AsyncTask <Void, Void, Void>
{
private ProgressDialog Dialog = new ProgressDialog(displayActivity.this);
protected void onPreExecute()
{
Dialog.setMessage(getString(R.string.dialog_wait_message));
Dialog.show();
}
protected void onPostExecute(Void unused)
{
Dialog.dismiss();
// displaying all the fetched data
}
@Override
protected Void doInBackground(Void... params)
{
// fetching data from web using HTTPGet method
return null;
}
}
Logcat output is:
java.lang.RuntimeException: Unable to destroy activity {myApplication/MyApplication.displayActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3789)
at android.app.ActivityThread.access$2400(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at MyApplication.displayActivity.onDestroy(displayActivity.java:74)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)
now, pls help me to fix this problem, how do i handle phone call or SMS received or such kind of interruptions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
步骤#1:找到
displayActivity.java
文件的第 74 行。步骤 #2:修复第 74 行中的任何问题。
现在,堆栈跟踪表明第 74 行位于
onDestroy()
中。假设您上面列出的活动代码来自displayActivity
,则第 74 行必须是:这意味着
listview1
为null
。您既不需要在上面列出的代码中声明这个数据成员,也不需要为其赋值。Step #1: Find line 74 of the
displayActivity.java
file.Step #2: Fix whatever problem is on line 74.
Now, the stack trace suggests that line 74 is in
onDestroy()
. Assuming that the activity code you have listed above is fromdisplayActivity
, then line 74 must be:That means
listview1
isnull
. You neither declare nor assign a value to this data member in the code you have listed above.尝试在 onDestroy 的第一行设置 BP 并验证 vars/members 以查找哪个为 null
try setting a BP at the first line of
onDestroy
and validate vars/members to find which one isnull