活动已泄露窗口com.android.internal.policy.impl.PhoneWindow$DecorView@405aeb60,最初添加在这里

时间:2013-05-21 09:58:35

标签: android android-alertdialog

我知道有很多关于这个话题的答案......但我无法找到解决问题的方法。它很简单,一个活动:

public class DialogsActivity extends Activity {

Bundle bundle=new Bundle();
String token;

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    mostrarDialogo();
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialogsactivitylayout);
    bundle=getIntent().getExtras();
    token=bundle.getString("token");
    token=token.replaceAll("\\s+","");
    Log.i("Parse", "Token: "+token);


}

private void mostrarDialogo() {
    if(token.equals("nologin")){
        AlertDialog.Builder nologin = new AlertDialog.Builder(DialogsActivity.this);
        nologin.setTitle(R.string.loginstring);
        nologin.setMessage(R.string.nologin);
        nologin.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {

                        dialog.dismiss();
                        finish();
                        return;
                    }

                });


        nologin.show(); 
    }else if(token.equals("nocon")){
        AlertDialog.Builder nocon = new AlertDialog.Builder(DialogsActivity.this);
        nocon.setTitle(R.string.loginstring);
        nocon.setMessage(R.string.timeout);
        nocon.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {

                        dialog.dismiss();
                        finish();
                        return;
                    }
                });
        nocon.show();

    }else if(token.equals("oklogin")){

        AlertDialog.Builder start=new AlertDialog.Builder(DialogsActivity.this);
        start.setTitle(R.string.warn);
        start.setMessage(R.string.service);
        start.setNegativeButton(R.string.accept,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                        finish();
                        return;
                    }
                });
        //finish();
        start.show();
    }else if(token!="nocon" && token!="nologin" && token!="oklogin"){
        AlertDialog.Builder error=new AlertDialog.Builder(DialogsActivity.this);
        error.setTitle(R.string.unknownerror);
        error.setMessage(R.string.unkerrortext);
        error.setPositiveButton(R.string.accept,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                        finish();
                        return;

                    }
                });
        error.show();
    }



}



}

运行Activity时,堆栈跟踪:

05-21 11:46:25.140: E/WindowManager(15737): Activity com.publidirecta.vinceriazafata.DialogsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405aeb60 that was originally added here
05-21 11:46:25.140: E/WindowManager(15737): android.view.WindowLeaked: Activity com.publidirecta.vinceriazafata.DialogsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405aeb60 that was originally added here
05-21 11:46:25.140: E/WindowManager(15737):     at android.view.ViewRoot.<init>(ViewRoot.java:263)
05-21 11:46:25.140: E/WindowManager(15737):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:171)
05-21 11:46:25.140: E/WindowManager(15737):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
05-21 11:46:25.140: E/WindowManager(15737):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.Dialog.show(Dialog.java:241)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.AlertDialog$Builder.show(AlertDialog.java:809)
05-21 11:46:25.140: E/WindowManager(15737):     at com.publidirecta.vinceriazafata.DialogsActivity.mostrarDialogo(DialogsActivity.java:88)
05-21 11:46:25.140: E/WindowManager(15737):     at com.publidirecta.vinceriazafata.DialogsActivity.onResume(DialogsActivity.java:20)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.Activity.performResume(Activity.java:3832)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-21 11:46:25.140: E/WindowManager(15737):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:46:25.140: E/WindowManager(15737):     at android.os.Looper.loop(Looper.java:130)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-21 11:46:25.140: E/WindowManager(15737):     at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:46:25.140: E/WindowManager(15737):     at java.lang.reflect.Method.invoke(Method.java:507)
05-21 11:46:25.140: E/WindowManager(15737):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-21 11:46:25.140: E/WindowManager(15737):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-21 11:46:25.140: E/WindowManager(15737):     at dalvik.system.NativeStart.main(Native Method)

错误总是在.show()方法上跳转,具体取决于它在一个if或另一个中输入的token变量的值。 而且,正如您所看到的,对话框总是被忽略。有帮助吗?谢谢!

3 个答案:

答案 0 :(得分:2)

你必须使用

dialog.create().show();

我修改了你的代码以使其变得简单

private void mostrarDialogo() {
    if(token.equals("nologin")){
    showDialog(R.string.loginstring,R.string.nologin,R.string.cancel)

    }else if(token.equals("nocon")){
    showDialog(R.string.loginstring,R.string.timeout,R.string.cancel);       


    }else if(token.equals("oklogin")){
    showDialog(R.string.warn,R.string.service,R.string.accept);

    }else if(token!="nocon" && token!="nologin" && token!="oklogin"){
    showDialog(R.string.unknownerror,R.string.unkerrortext,R.string.accept);
   }

}

void showDialog(String title,String msg,String buttonText)
{
      AlertDialog.Builder dialog=new AlertDialog.Builder(DialogsActivity.this);
        dialog.setTitle(title);
        dialog.setMessage(msg);
        dialog.setPositiveButton(buttonText,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    finish();
                    return;

                }
            });
    dialog.create().show();

}

答案 1 :(得分:1)

您应该在显示之前创建对话框。

例如:

nocon.create().show();

代替

nocon.show();

答案 2 :(得分:-1)

问题似乎与您在Context中创建AlertDialog的{​​{1}}对象有关。 每当您需要传递/使用onResume()时,始终建议尽可能使用Context对象,而不是getApplicationContext()

ActivityContext替换DialogsActivity.this应该可以正常使用。 希望这会有所帮助。