1

我有以下用于显示自定义弹出窗口的代码,但是执行相同的代码会导致窗口泄漏错误,我无法确定如何解决它。以下是我的代码:

    public void Reminder() {
    dDialog = new Dialog(Farrier.this);
    dDialog.setContentView(R.layout.farrierpop2);
    dDialog.setTitle("Reminder");
    dDialog.setCancelable(true);
    final Button pop1 = (Button) dDialog.findViewById(R.id.Btn1);
    pop1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            intent.putExtra("title", IDB);
            intent.putExtra("description", "Farrier Service");
            intent.putExtra("beginTime", "eventStartInMillis");
            intent.putExtra("endTime", "eventEndInMillis");
            startActivity(intent);
            finish();
        }
    });
    Button pop2 = (Button) dDialog.findViewById(R.id.Btn2);
    pop2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // dDialog.dismiss();
            Intent ourpartner = new Intent(Farrier.this, FarrierSch.class);
            startActivity(ourpartner);
            finish();
        }
    });
    dDialog.show();
}

堆栈跟踪是:

*07-01 11:30:18.646: E/WindowManager(407): Activity com.gallops.Farrier has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45fe7468 that was originally added here
07-01 11:30:18.646: E/WindowManager(407): android.view.WindowLeaked: Activity com.gallops.Farrier has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45fe7468 that was originally added here
07-01 11:30:18.646: E/WindowManager(407):   at android.view.ViewRoot.<init>(ViewRoot.java:227)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-01 11:30:18.646: E/WindowManager(407):   at android.app.Dialog.show(Dialog.java:239)
07-01 11:30:18.646: E/WindowManager(407):   at com.gallops.Farrier.Reminder(Farrier.java:185)
07-01 11:30:18.646: E/WindowManager(407):   at com.gallops.Farrier$2.onClick(Farrier.java:915)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.View.performClick(View.java:2364)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.View.onTouchEvent(View.java:4179)
07-01 11:30:18.646: E/WindowManager(407):   at android.widget.TextView.onTouchEvent(TextView.java:6541)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.View.dispatchTouchEvent(View.java:3709)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 11:30:18.646: E/WindowManager(407):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
07-01 11:30:18.646: E/WindowManager(407):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-01 11:30:18.646: E/WindowManager(407):   at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-01 11:30:18.646: E/WindowManager(407):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-01 11:30:18.646: E/WindowManager(407):   at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-01 11:30:18.646: E/WindowManager(407):   at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 11:30:18.646: E/WindowManager(407):   at android.os.Looper.loop(Looper.java:123)
07-01 11:30:18.646: E/WindowManager(407):   at android.app.ActivityThread.main(ActivityThread.java:4363)
07-01 11:30:18.646: E/WindowManager(407):   at java.lang.reflect.Method.invokeNative(Native Method)
07-01 11:30:18.646: E/WindowManager(407):   at java.lang.reflect.Method.invoke(Method.java:521)
07-01 11:30:18.646: E/WindowManager(407):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-01 11:30:18.646: E/WindowManager(407):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-01 11:30:18.646: E/WindowManager(407):   at dalvik.system.NativeStart.main(Native Method)*
4

4 回答 4

1

尝试dismissdialogexisting当前activity

 if(dDialog.isShowing()){
   dDialog.dismiss();
   }
于 2013-07-01T11:50:28.950 回答
1

您正在完成对话框的父活动,您必须首先关闭对话框,并且您必须将 onCancleListener 设置为您的对话框,然后在 onCancleListener 中启动其他活动并完成此活动

dDialog.setOnCancleListener();

@Override
onCancle()
{
startactivityone();

OR

startactivitytwo();
}

public void Reminder() {
dDialog = new Dialog(Farrier.this);
dDialog.setContentView(R.layout.farrierpop2);
dDialog.setTitle("Reminder");
dDialog.setCancelable(true);
final Button pop1 = (Button) dDialog.findViewById(R.id.Btn1);
pop1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("title", IDB);
        intent.putExtra("description", "Farrier Service");
        intent.putExtra("beginTime", "eventStartInMillis");
        intent.putExtra("endTime", "eventEndInMillis");
        startActivity(intent);
        finish();
    }
});
Button pop2 = (Button) dDialog.findViewById(R.id.Btn2);
pop2.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // dDialog.dismiss();
        Intent ourpartner = new Intent(Farrier.this, FarrierSch.class);
        startActivity(ourpartner);
        finish();
    }
});
dDialog.show();
}
于 2013-07-01T11:44:24.737 回答
0

使用对话框片段。对话框已弃用。(可能是由于这些问题)。

于 2013-07-01T11:40:01.853 回答
0

就我而言,我在弹出窗口中调用了 finish() 。这是一个视频播放器,它显示一个标题栏,顶部有一个关闭按钮。单击弹出窗口中的关闭按钮后,活动将关闭。

这显然导致了泄漏,因为在调用 finish() 时弹出窗口仍然显示。通过检查弹出窗口是否在 parentActivity 中可见并关闭它来解决这个问题

@Override
    protected void onDestroy() {
        super.onDestroy();
        if (mVideoView != null) {
            mVideoView = null;

        }
        if(mMediaController.isShowing()){
            mMediaController.hide();
            mMediaController=null;          
        }
    }
于 2014-06-12T04:18:50.807 回答