android.view.WindowManager$BadTokenException

1860 ワード

最近、カスタムダイアログの機能をしましたが、上記のエラーを報告することがよくあります.ログ情報を見ると、alertDialogにあるためです.show()の場合の問題は,現在のActivityが存在するか,現在のActivityが存在しない場合に異常が報告されるため,alertDialogを解決する方法である.show()try{...}catch{...};もう1つはalertDialogですshow()前に、現在のActivityが存在するかどうかを判断し、if(!TestEnvironment.this.isFinishing())
//  
 private void showDialog() {
  final AlertDialog.Builder builder;
  final AlertDialog alertDialog;
  LayoutInflater inflater = (LayoutInflater)context
    .getSystemService(LAYOUT_INFLATER_SERVICE);
  View layout = inflater.inflate(R.layout.dialog,null);
 
  TextView dialogTitle = (TextView) layout
    .findViewById(R.id.dialogtitle_id);
 
  if (sysFlag) {
   dialogTitle.setText(dialogsysStr);
  } else if (softFlag) {
   dialogTitle.setText(dialogSoftStr);
  }
 
  builder = new AlertDialog.Builder(context);
  alertDialog = builder.create();
  //  setview alertdialog , 
  alertDialog.setView(layout, 0, 0, 0, 0);
  
  //
//  try {// 
  if(!TestEnvironment.this.isFinishing())   // 
   alertDialog.show();
//  } catch (Exception e) {
//   // TODO: handle exception
//   finish();
//  }
  
 
  Button butDialog_ok = (Button) layout
    .findViewById(R.id.dialog_button_ok);
  Button butDialog_cancel = (Button) layout
    .findViewById(R.id.dialog_button_cancel);
  
  butDialog_ok.setOnClickListener(new View.OnClickListener() {
 
   @Override
   public void onClick(View v) {
 
    if (sysFlag) {
     update.openDownloadUpdateUI(eUpdateType.SYSTEM_UPDATE,
       TestEnvironment.this); //  
    } else if (softFlag) {
     update.openDownloadUpdateUI(eUpdateType.APP_UPDATE,
       TestEnvironment.this); //  
    }
    alertDialog.dismiss();
 
   }
  });
 
  butDialog_cancel.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    alertDialog.dismiss();
   }
  });
 
 }