2010-09-22 46 views
11

我的应用程序频繁抛出异常,如下图所示:我的应用程序经常会抛出异常android.view.WindowLeaked -

E /窗口管理器(6282):android.view.WindowLeaked:活动 com.myActivity已泄漏窗口 [email protected]是 原来这里

添加的应用程序显示在主活动启动,并开始一个任务进度对话框。任务完成后,它将关闭进度对话框。

我的代码如下所示。有人能帮我吗?

public class MyActivity extends Activity { 

private static int ID_DIALOG_PROGRESS = 2001; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.my_activity); 
    showDialog(ID_DIALOG_PROGRESS); 
    new MyTask().execute(null, null, null); 
} 

@Override 
protected Dialog onCreateDialog(int id) { 
    if (id == ID_DIALOG_PROGRESS) { 
     ProgressDialog loadingDialog = new ProgressDialog(this); 
     loadingDialog.setTitle(""); 
     loadingDialog.setMessage(""); 
     loadingDialog.setIndeterminate(true); 
     loadingDialog.setCancelable(false); 
     return loadingDialog; 
    } 

    return super.onCreateDialog(id); 
} 

private class MyTask extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... arg0) { 

      /* Do something expensive here...*/ 

      /* Start other activity*/ 
      Intent intent = new Intent(MyActivity.this, OtherActivity.class); 
      startActivityForResult(intent, 1000); 
     } 

     return null; 
    } 

    protected void onPostExecute(Void arg0) { 
     dismissDialog(ID_DIALOG_PROGRESS); 
    } 
} 
} 

大多数情况下,showDialog()调用引发异常。另一次,从dismissDialog()调用中抛出异常。

预先感谢您!

回答

6

在关闭对话框onPostExecute()之前,您正在开始一个新的活动doInBackground(),这可能是导致对话框泄漏的原因。在dismissDialog()电话后,我会提出

Intent intent = new Intent(MyActivity.this, OtherActivity.class); 
startActivityForResult(intent, 1000); 

onPostExecute()看看会发生什么。

0

这也是很好的做法,把一个try ... catch周围

dismissDialog(ID_DIALOG_PROGRESS); 

否则你可能会得到随机的应用程序崩溃时,在某些情况下该对话框不再提供例如屏幕旋转后。