2014-10-01 68 views
1

我想從我自己運行另一個應用程序。如果沒有安裝其他應用程序,那麼我想要一個對話框來詢問你是否要安裝它,如果你這樣做,那麼它將爲你打開市場。但是,我得到這個錯誤。這個錯誤是什麼?是什麼原因造成的?我該如何解決?Android - AlertDialog導致android.view.WindowsLeaked

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    barcodeAlive = true; 

    try { 

     startActivityForResult(new Intent(
       "com.google.zxing.client.android.SCAN").putExtra(
       "SCAN_MODE", "QR_CODE_MODE"), 0); 
    } catch (ActivityNotFoundException anfe) { 

     new Handler().postDelayed(new Runnable() { 

      @Override 
      public void run() { 

       showDownloadDialog(BarcodeActivity.this, 
         "QR Reader could not found..", 
         "Do you want to download QR Reader ? ", "Yes", "No"); 
      } 
     }, DELAY); 
    } 
} 

private static AlertDialog showDownloadDialog(final Activity activity, 
     CharSequence stringTitle, CharSequence stringMessage, 
     CharSequence stringButtonYes, CharSequence stringButtonNo) { 

    final AlertDialog.Builder downloadDialog = new AlertDialog.Builder(
      activity); 
    downloadDialog.setTitle(stringTitle); 
    downloadDialog.setMessage(stringMessage); 
    downloadDialog.setPositiveButton(stringButtonYes, 
      new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 

        Uri uri = Uri.parse("market://search?q=pname:" 
          + PACKAGE); 
        try { 
         activity.startActivity(new Intent(
           Intent.ACTION_VIEW, uri)); 
        } catch (ActivityNotFoundException anfe) { 

        } catch (Exception e) { 
        } 
       } 
      }); 
    downloadDialog.setNegativeButton(stringButtonNo, 
      new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 

        downloadDialog 
          .getContext() 
          .startActivity(
            new Intent(downloadDialog.getContext(), 
              MainActivity.class) 
              .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)); 
       } 
      }); 
    downloadDialog.setCancelable(false); 
    return downloadDialog.show(); 
} 
+0

[活動泄漏了最初添加的窗口]的可能重複(http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added) – 2014-10-01 14:26:33

回答

1

你應該dismiss()你的對話在開始之前Activity。 如果將其打開,您將泄漏窗口。