窗口泄漏与alertdialog

时间:2013-02-13 11:34:24

标签: android alertdialog

我有一个应用程序,可以在手机的数据库中查询消息。如果光标返回其中包含消息,那么我试图一个接一个地在alertdialog中显示每条消息。我收到以下错误,说有窗口泄漏。

我认为这与在下一个显示之前未正确取消alertdialog有关。有谁知道我为什么会收到这个错误?

@Override
    protected void onResume() {
        super.onResume();
      Cursor c = nfcScannerApplication.loginValidate.queryAllFromMessage();

        Log.e(TAG, "messages in db = " +  c.getCount());


            if(c != null && c.getCount() > 0){
                c.moveToFirst();

                while(c.moveToNext()){

                final String guid = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_GUID));
                final String message = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_TEXT));
                final String createdAt = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_CREATED_AT));
                final String sender = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_SENDER));


                DateTime dt = new DateTime(createdAt);
                DateTimeFormatter fmt2 = DateTimeFormat.forPattern("H:mm d-MMM-Y");
                String date = fmt2.print(dt);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        this);

                    // set title
               alertDialogBuilder.setTitle("You have a new message : " );

                    // set dialog message
                    alertDialogBuilder
                        .setMessage(  "\n" + message + "\n"
                                + "From : " + sender + " at " + date + "\n"+"Click OK to confirm message read")
                        .setCancelable(false)
                        .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {


                                nfcScannerApplication.loginValidate.deleteMessage(guid);

                                Cursor c = nfcScannerApplication.loginValidate.queryAllFromMessage();
                                Log.e(TAG, "message count = " + c.getCount());

                                //send guid back to martin so he can delete message on server
                                String[] params = new String[]{guid};
                                AsyncCompleteMessage ascm = new AsyncCompleteMessage();
                                ascm.execute(params);

                                finish();

                            }
                          });

                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create(); 

                        // show it
                        alertDialog.show();

                }//end of while
            }else{

                finish();
            }

    }

02-13 11:33:13.602: E/WindowManager(19605): Activity com.carefreegroup.DisplayMessageActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@418e0ac8 that was originally added here
02-13 11:33:13.602: E/WindowManager(19605): android.view.WindowLeaked: Activity com.carefreegroup.DisplayMessageActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@418e0ac8 that was originally added here
02-13 11:33:13.602: E/WindowManager(19605):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:465)
02-13 11:33:13.602: E/WindowManager(19605):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:419)
02-13 11:33:13.602: E/WindowManager(19605):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:351)
02-13 11:33:13.602: E/WindowManager(19605):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:171)
02-13 11:33:13.602: E/WindowManager(19605):     at android.view.Window$LocalWindowManager.addView(Window.java:558)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.Dialog.show(Dialog.java:282)
02-13 11:33:13.602: E/WindowManager(19605):     at com.carefreegroup.DisplayMessageActivity.onResume(DisplayMessageActivity.java:103)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1266)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.Activity.performResume(Activity.java:5148)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2997)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3055)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread.access$600(ActivityThread.java:151)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
02-13 11:33:13.602: E/WindowManager(19605):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 11:33:13.602: E/WindowManager(19605):     at android.os.Looper.loop(Looper.java:155)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread.main(ActivityThread.java:5485)
02-13 11:33:13.602: E/WindowManager(19605):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 11:33:13.602: E/WindowManager(19605):     at java.lang.reflect.Method.invoke(Method.java:511)
02-13 11:33:13.602: E/WindowManager(19605):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
02-13 11:33:13.602: E/WindowManager(19605):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
02-13 11:33:13.602: E/WindowManager(19605):     at dalvik.system.NativeStart.main(Native Method)
02-13 11:33:13.602: E/WindowManager(19605): Activity com.carefreegroup.DisplayMessageActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4152c258 that was originally added here
02-13 11:33:13.602: E/WindowManager(19605): android.view.WindowLeaked: Activity com.carefreegroup.DisplayMessageActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4152c258 that was originally added here
02-13 11:33:13.602: E/WindowManager(19605):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:465)
02-13 11:33:13.602: E/WindowManager(19605):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:419)
02-13 11:33:13.602: E/WindowManager(19605):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:351)
02-13 11:33:13.602: E/WindowManager(19605):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:171)
02-13 11:33:13.602: E/WindowManager(19605):     at android.view.Window$LocalWindowManager.addView(Window.java:558)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.Dialog.show(Dialog.java:282)
02-13 11:33:13.602: E/WindowManager(19605):     at com.carefreegroup.DisplayMessageActivity.onResume(DisplayMessageActivity.java:103)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1266)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.Activity.performResume(Activity.java:5148)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2997)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3055)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread.access$600(ActivityThread.java:151)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
02-13 11:33:13.602: E/WindowManager(19605):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 11:33:13.602: E/WindowManager(19605):     at android.os.Looper.loop(Looper.java:155)
02-13 11:33:13.602: E/WindowManager(19605):     at android.app.ActivityThread.main(ActivityThread.java:5485)
02-13 11:33:13.602: E/WindowManager(19605):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 11:33:13.602: E/WindowManager(19605):     at java.lang.reflect.Method.invoke(Method.java:511)
02-13 11:33:13.602: E/WindowManager(19605):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
02-13 11:33:13.602: E/WindowManager(19605):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
02-13 11:33:13.602: E/WindowManager(19605):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:5)

dialog.dismiss();方法中调用finish()之前尝试拨打onClick(),如下面的代码所示。

public void onClick(DialogInterface dialog,int id) {


                            nfcScannerApplication.loginValidate.deleteMessage(guid);

                            Cursor c = nfcScannerApplication.loginValidate.queryAllFromMessage();
                            Log.e(TAG, "message count = " + c.getCount());

                            //send guid back to martin so he can delete message on server
                            String[] params = new String[]{guid};
                            AsyncCompleteMessage ascm = new AsyncCompleteMessage();
                            ascm.execute(params);

                            dialog.dismiss();
                            finish();

                        }

答案 1 :(得分:0)

您在退出活动后尝试显示对话框。请检查此link.