Android泄露了窗口

时间:2015-11-05 09:16:04

标签: android memory-leaks

我想在完成活动之前显示一个对话框。我显示了对话框,按下OK按钮后活动将关闭。但我得到一个窗口泄漏错误。这是我的代码。

主要活动

    @Override
    public void onBackPressed(){
         FragmentManager fm = getFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
            Log.i("MainActivity", "popping backstack");
            fm.popBackStack();
        } else {
            MyAlertDialog dialog=new MyAlertDialog(this);
            dialog.displayDialog("Exit", "Do you wish to exit application?",0);
            super.onBackPressed();
        }
    } 

Dialog Class

public void displayDialog(String title,String message,final int flag)
{
     AlertDialog.Builder builder=new AlertDialog.Builder(context);
     builder.setTitle(title)
         .setMessage(message);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {              
            @Override
            public void onClick(DialogInterface dialog, int which) 
             {
                dialog.dismiss();
                 passIntent(flag);
            }
        });

        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {              
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

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


public void passIntent(int flag)
{
    switch(flag)
     {
     case 0:
        ((Activity) context).finish();
        break;
    }
}

这是我的错误

11-05 13:48:23.935: E/WindowManager(1353): android.view.WindowLeaked: Activity com.mcommerce.main.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{26da0ed7 V.E..... R.....ID 0,0-480,284} that was originally added here
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
11-05 13:48:23.935: E/WindowManager(1353):  at android.app.Dialog.show(Dialog.java:298)
11-05 13:48:23.935: E/WindowManager(1353):  at com.mcommerce.dialog.MyAlertDialog.displayDialog(MyAlertDialog.java:63)
11-05 13:48:23.935: E/WindowManager(1353):  at com.mcommerce.main.MainActivity.onBackPressed(MainActivity.java:256)
11-05 13:48:23.935: E/WindowManager(1353):  at android.app.Activity.onKeyUp(Activity.java:2456)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.KeyEvent.dispatch(KeyEvent.java:2643)
11-05 13:48:23.935: E/WindowManager(1353):  at android.app.Activity.dispatchKeyEvent(Activity.java:2707)
11-05 13:48:23.935: E/WindowManager(1353):  at android.support.v7.internal.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:49)
11-05 13:48:23.935: E/WindowManager(1353):  at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:258)
11-05 13:48:23.935: E/WindowManager(1353):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2276)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4020)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3982)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3713)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3874)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2208)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1849)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1840)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2185)
11-05 13:48:23.935: E/WindowManager(1353):  at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
11-05 13:48:23.935: E/WindowManager(1353):  at android.os.MessageQueue.nativePollOnce(Native Method)
11-05 13:48:23.935: E/WindowManager(1353):  at android.os.MessageQueue.next(MessageQueue.java:143)
11-05 13:48:23.935: E/WindowManager(1353):  at android.os.Looper.loop(Looper.java:122)
11-05 13:48:23.935: E/WindowManager(1353):  at android.app.ActivityThread.main(ActivityThread.java:5257)
11-05 13:48:23.935: E/WindowManager(1353):  at java.lang.reflect.Method.invoke(Native Method)
11-05 13:48:23.935: E/WindowManager(1353):  at java.lang.reflect.Method.invoke(Method.java:372)
11-05 13:48:23.935: E/WindowManager(1353):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-05 13:48:23.935: E/WindowManager(1353):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我提到了很多链接,但我仍然无法解决这个问题。有人请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您的问题似乎是由于您在显示Activity后立即离开Dialog

@Override
public void onBackPressed(){
     FragmentManager fm = getFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        Log.i("MainActivity", "popping backstack");
        fm.popBackStack();
    } else {
        MyAlertDialog dialog=new MyAlertDialog(this);
        dialog.displayDialog("Exit", "Do you wish to exit application?",0);
        // super.onBackPressed(); // <-- remove this and implement closing of the activity in the Dialog logic
    }
} 

答案 1 :(得分:0)

错误:

  

android.view.WindowLeaked:Activity com.mcommerce.main.MainActivity   已经泄露了窗户   com.android.internal.policy.impl.PhoneWindow $ {DecorView 26da0ed7   最初添加的V.E ..... R ..... ID 0,0-480,284}

您收到此错误是因为在Dialog完成Activity后显示super.onBackPressed()

将显示您的对话框,并且还会调用MyAlertDialog dialog=new MyAlertDialog(this); dialog.displayDialog("Exit", "Do you wish to exit application?",0); 来完成您的活动。

试试这个

MyAlertDialog dialog=new MyAlertDialog(this);
dialog.displayDialog("Exit", "Do you wish to exit application?",0);
super.onBackPressed();

而不是

super.onBackPressed()

所以请删除Route::match(['get', 'post'], '/users/{nationalCode}', ['as' => 'admin.user.revoke_user', 'uses' => 'AdminUsersController@revokeUser']); 然后它会解决您的问题。