自定义的弹出窗口导致泄露的窗口。

时间:2013-07-01 11:36:26

标签: android android-dialog

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

    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 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

尝试dismiss dialog existing activity当前 if(dDialog.isShowing()){ dDialog.dismiss(); }

{{1}}

答案 2 :(得分:0)

使用对话框片段。不建议使用对话框。 (可能是由于那些问题)。

答案 3 :(得分:0)

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

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

来对此进行排序
@Override
    protected void onDestroy() {
        super.onDestroy();
        if (mVideoView != null) {
            mVideoView = null;

        }
        if(mMediaController.isShowing()){
            mMediaController.hide();
            mMediaController=null;          
        }
    }