Android“android.view.WindowLeaked:”异常

时间:2012-01-13 23:32:19

标签: android exception view sdk window

我已经制作了这段代码。只要在对话框中按下“确定”按钮,应用程序就会崩溃并显示异常。我无法弄清楚为什么,任何帮助?我已按照Android文档中的这些说明进行操作。

package com.grawl.faqplus;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FAQPlusActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Set up buttons

        Button buttonExit = (Button) findViewById(R.id.button_exit);
        Button buttonAbout = (Button) findViewById(R.id.button_about);

        // Set up AlertDialog for buttonAbout

        String aboutMessage = (String) getString(R.string.dialog_about);

        AlertDialog.Builder builderAbout = new AlertDialog.Builder(this);
        builderAbout.setMessage(aboutMessage);
        builderAbout.setCancelable(false);
        builderAbout.setNeutralButton("OK!", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                finish();
            }
        });
        final AlertDialog alertAbout = builderAbout.create();

        // Show about dialog

        buttonAbout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                alertAbout.show();
            }
        });

        // Exit app

        buttonExit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
                System.exit(0);
            }
        });      
    }
}

1 个答案:

答案 0 :(得分:2)

我通过替换

修复了它
finish();

dialog.dismiss();