甚至在dialog.dismiss()上泄漏窗口
我正在使用此代码:
public void Close() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch(which) {
case DialogInterface.BUTTON_POSITIVE:
dialog.dismiss();
MainActivity.this.finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
dialog.cancel();
break;
}
}
};
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Thanks");
builder.setPositiveButton("Quit", dialogClickListener);
builder.setNegativeButton("Stay", dialogClickListener);
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
builder.show();
}
});
}
当用户选择退出时调用Close()。 它会打开一个警报框“构建器”。 当我在构建器上选择“退出”时,出现此错误:
06-23 18:32:25.230: E/WindowManager(532): Activity com.amateurprogs.championpkg.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41768940 that was originally added here 06-23 18:32:25.230: E/WindowManager(532): android.view.WindowLeaked: Activity com.amateurprogs.championpkg.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41768940 that was originally added here 06-23 18:32:25.230: E/WindowManager(532): at android.view.ViewRootImpl.(ViewRootImpl.java:344) 06-23 18:32:25.230: E/WindowManager(532): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267) 06-23 18:32:25.230: E/WindowManager(532): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 06-23 18:32:25.230: E/WindowManager(532): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 06-23 18:32:25.230: E/WindowManager(532): at android.view.Window$LocalWindowManager.addView(Window.java:537) 06-23 18:32:25.230: E/WindowManager(532): at android.app.Dialog.show(Dialog.java:278) 06-23 18:32:25.230: E/WindowManager(532): at android.app.AlertDialog$Builder.show(AlertDialog.java:932) 06-23 18:32:25.230: E/WindowManager(532): at com.amateurprogs.championpkg.MainActivity$2.run(MainActivity.java:42)
MainActivity.java的第42行指向builder.show()。
在选择了“退出”按钮,我做了dialog.dismiss(),然后Activity.finish()。 我仍然有一个窗户泄漏。
为什么即使在dialog.dismiss()上也会出现此错误。
所以起初,你的代码写得很脏。 没有理由将你的监听器包装到close()
方法中,只需声明OnClickListener
就像你声明普通方法一样。
其次,也没有理由在runOnUiThread
调用dialog.show()
。 就在用户点击Quit Button
,只调用dialog.show()
。
改变我的意思。
更新:这里是工作示例:
package com.sajmon.test; // sajmon is my old nick
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class TestActivity extends Activity implements View.OnClickListener {
protected static final int CREATE_CLOSE_DIALOG = 1245;
protected Button someBtn;
protected AlertDialog diag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
someBtn = (Button) findViewById(R.id.btn);
someBtn.setOnClickListener(this);
}
public void onClick(View view) {
if (view.getId() == R.id.btn) {
diag = createDialog(CREATE_CLOSE_DIALOG);
diag.show();
}
}
private AlertDialog createDialog(int type) {
AlertDialog dialog = null;
switch (type) {
case CREATE_CLOSE_DIALOG:
dialog = new AlertDialog.Builder(TestActivity.this)
.setTitle("Closing")
.setMessage("Do you want to quit?")
.setPositiveButton("Yes", dialogListener)
.setNegativeButton("no", dialogListener)
.create();
break;
}
return dialog;
}
final DialogInterface.OnClickListener dialogListener = new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
dialog.dismiss();
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
dialog.dismiss();
break;
}
}
};
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Quit!"
android:layout_marginTop="20dp"
/>
</LinearLayout>
谢谢回答我的问题。 实际上,我从非活动类调用Close(),因此调用runOnUiThread()。 我期待在调用activity.finish()之前,dialog.dismiss()将完成其工作。 无论如何,我将使用dialog.builder.create()并尝试您的解决方案。
您需要关闭onPause()
或onDestroy()
方法中的对话框