我正在使用此代码:
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()上也会出现此错误。
答案 0 :(得分:0)
首先,您的代码是脏写的。没有理由将您的监听器包装到close()
方法,只需声明OnClickListener
,就像声明普通方法一样。
第二,也没有理由在dialog.show()
中致电runOnUiThread
。用户点击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>
答案 1 :(得分:0)
感谢您回答我的问题。实际上,我从非活动类调用Close()因此是runOnUiThread()。 我期待dialog.dismiss()在调用activity.finish()之前完成它的工作。 无论如何,我将使用dialog.builder.create()并尝试您的解决方案。
答案 2 :(得分:0)
您需要在onPause()
或onDestroy()
方法