Android Alert对话框无需等待即可关闭

时间:2013-04-02 10:02:15

标签: android

在我的应用程序中,我使用警告对话框显示警告消息并等待是/否。但警报对话框显示无需等待并消失。这里的代码如下所示

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Delete Macro");
            builder.setMessage(" " + mMacroName + "\n\nAre you sure ?");

            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                   // some tasks
                   }
               });

            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       finish();
                   }
               });

            AlertDialog dialog = builder.create();
            builder.show();

收到logcat 显示出一些泄漏

 04-02 15:09:32.954: E/WindowManager(279): Activity com.mtxlabs.hat.setup.MacroProp has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43ea4d50 that was originally added here
 04-02 15:09:32.954: E/WindowManager(279): android.view.WindowLeaked: Activity com.mtxlabs.hat.setup.MacroProp has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43ea4d50 that was originally added here
 04-02 15:09:32.954: E/WindowManager(279):  at android.view.ViewRoot.<init>(ViewRoot.java:247)
 04-02 15:09:32.954: E/WindowManager(279):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
 04-02 15:09:32.954: E/WindowManager(279):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
 04-02 15:09:32.954: E/WindowManager(279):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
 04-02 15:09:32.954: E/WindowManager(279):  at android.app.Dialog.show(Dialog.java:241)
 04-02 15:09:32.954: E/WindowManager(279):  at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
 04-02 15:09:32.954: E/WindowManager(279):  at com.mtxlabs.hat.setup.MacroProp.onSaveButtonClick(MacroProp.java:218)
 04-02 15:09:32.954: E/WindowManager(279):  at java.lang.reflect.Method.invokeNative(Native Method)
 04-02 15:09:32.954: E/WindowManager(279):  at java.lang.reflect.Method.invoke(Method.java:521)
 04-02 15:09:32.954: E/WindowManager(279):  at android.view.View$1.onClick(View.java:2067)
 04-02 15:09:32.954: E/WindowManager(279):  at android.view.View.performClick(View.java:2408)
 04-02 15:09:32.954: E/WindowManager(279):  at android.view.View$PerformClick.run(View.java:8816)
 04-02 15:09:32.954: E/WindowManager(279):  at android.os.Handler.handleCallback(Handler.java:587)
 04-02 15:09:32.954: E/WindowManager(279):  at android.os.Handler.dispatchMessage(Handler.java:92)
 04-02 15:09:32.954: E/WindowManager(279):  at android.os.Looper.loop(Looper.java:123)
 04-02 15:09:32.954: E/WindowManager(279):  at android.app.ActivityThread.main(ActivityThread.java:4627)
 04-02 15:09:32.954: E/WindowManager(279):  at java.lang.reflect.Method.invokeNative(Native Method)
 04-02 15:09:32.954: E/WindowManager(279):  at java.lang.reflect.Method.invoke(Method.java:521)
 04-02 15:09:32.954: E/WindowManager(279):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 04-02 15:09:32.954: E/WindowManager(279):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 04-02 15:09:32.954: E/WindowManager(279):  at dalvik.system.NativeStart.main(Native Method)

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

试试这个

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Delete Macro");
                builder.setMessage(" " + mMacroName + "\n\nAre you sure ?");

                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                       // some tasks
                       }
                   })

                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {

                       //you are finishing the activity first and trying to display          the dialog later.this your problem 
                           // comment the `finish()`    and see

                          //finish();
                       }
                   }).create();

                builder.show();