Android - AlertDialog导致android.view.WindowLeaked

时间:2014-10-01 14:12:09

标签: android alertdialog

我正在尝试自己运行另一个应用程序。如果没有安装其他应用程序,那么我想要一个弹出的对话框,询问您是否要安装它,如果您这样做,那么它将为您打开市场。但是,我收到此错误。这个错误是什么?是什么导致它,我该如何解决?

@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();
}

1 个答案:

答案 0 :(得分:1)

在启动其他dismiss()之前,您应该Activity您的对话框。 如果你把它打开,你会泄漏窗户。