在异步任务中给出异常的进度对话框[重复]

Posted

技术标签:

【中文标题】在异步任务中给出异常的进度对话框[重复]【英文标题】:progress dialogue box giving exception in asynchtask [duplicate] 【发布时间】:2013-04-17 19:48:29 【问题描述】:

我正在开发一个 android 应用程序,当我关闭进度对话框时,我正在使用异步任务和后期执行方法,异常即将到来并且应用程序正在强制关闭。

异常来了:

    04-24 09:41:54.661: E/AndroidRuntime(1727): java.lang.IllegalArgumentException: View not attached to window manager
    04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)

代码:

data_insertion = new AsyncTask<Void, Void, Void>() 
  @Override
  protected void onPreExecute() 
    // TODO Auto-generated method stub             
    CommonUtility.show_PDialog(MainActivity.this);
    super.onPreExecute();
  
  @Override
  protected void onPostExecute(Void result) 
    // TODO Auto-generated method stub
    //setting alaram for refresh api 
    CommonUtility.close_PDialog(); //*getting exception on this line* 
    Intent setalaram = new Intent(MainActivity.this, SetAlaram.class);
    startService(setalaram);
    Intent i = new Intent(MainActivity.this, PlayListActivity.class);
    startActivity(i);
    MainActivity.this.finish();
    super.onPostExecute(result);
    finish();
  
  @Override
  protected Void doInBackground(Void...params) 
    // TODO Auto-generated method stub
    //some code 
  
  return null;

.execute(null, null, null); 

//and here is my close method for dilogue
public static void close_PDialog() 
  if (dialog != null && dialog.isShowing()) 
    dialog.dismiss();
  

日志输出:

04-24 09:41:54.661: E/AndroidRuntime(1727): FATAL EXCEPTION: main
04-24 09:41:54.661: E/AndroidRuntime(1727): java.lang.IllegalArgumentException: View not attached to window manager
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.view.Window$LocalWindowManager.removeView(Window.java:432)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.app.Dialog.dismissDialog(Dialog.java:278)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.app.Dialog.access$000(Dialog.java:71)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.app.Dialog$1.run(Dialog.java:111)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.app.Dialog.dismiss(Dialog.java:268)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at commonUtilities.CommonUtility.close_PDialog(CommonUtility.java:233)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at com.walkover.filesharing.MainActivity$1.onPostExecute(MainActivity.java:55)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at com.walkover.filesharing.MainActivity$1.onPostExecute(MainActivity.java:1)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.os.AsyncTask.finish(AsyncTask.java:417)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.os.Looper.loop(Looper.java:130)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at java.lang.reflect.Method.invoke(Method.java:507)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

CommonUtility.close_PDialog();方法的邮政编码。 请发布 logcat 输出? 如果去掉抛出异常的那一行,progressDialog是停留在屏幕上还是消失了? “对话”对象在哪里实例化?在 show_PDialog(MainActivity.this) 里面? @Analizer 我按照你说的做了,这次出乎意料地出现了异常,进度条也消失了。首先非常感谢,你能告诉我这个的原因..为什么会这样?? 【参考方案1】:

可能当任务结束并在其 onPostExecute 上运行时,创建它(并从中获取上下文)的 Activity 在到达 onPostExecute 时已经被销毁。

您可以保持这种方式,或者在某个地方创建一个 progressDialog 实例,以便每次您需要一个对话框时都可以使用它,并且在您的活动的 onDestroy() 方法中您可以取消它(以防发生类似情况)。

【讨论】:

以上是关于在异步任务中给出异常的进度对话框[重复]的主要内容,如果未能解决你的问题,请参考以下文章

使用异步任务制作进度对话框

异步任务进度对话框需要很长时间才能完成 android 中的简单任务

异步任务的进度对话框

尝试连接到服务器时带有异步任务的进度条

我如何在我的异步任务类中使用圆形旋转进度条

如何关闭带有异步任务的进度对话框并避免“您的活动是不是运行错误?”