我知道在stackoverflow上多次询问此问题,但我无法找到可以解决问题的答案。
这是我从我的Activity的onPause()函数调用的代码。
`
`protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
new DialogueTask().execute(this);
}
Dialogue类任务的实现是
class DialogueTask extends AsyncTask<Activity, Void, PopupWindow> {
private View layout;
@Override
protected PopupWindow doInBackground(Activity... activities) {
LayoutInflater layoutInflater = (LayoutInflater) activities[0]
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup,
(ViewGroup) activities[0].findViewById(R.id.popup_id));
PopupWindow pw;
pw = new PopupWindow(layout, 100, 100, true);
return pw;
}
@Override
protected void onPostExecute(PopupWindow pw) {
// TODO Auto-generated method stub
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
这是popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Sure you want to Quit?" >
</TextView>
</LinearLayout>
单击后退按钮,然后调用onPause()并发出WindowLeaked异常。我想,我已经注意到通过实现AsyncTask在UI线程中显示了弹出窗口。那问题可能在哪里?
答案 0 :(得分:0)
弹出窗口活动从主活动打开,您将退出主活动而不会忽略弹出窗口活动。 pw.showAtLocation(layout,Gravity.CENTER,0,0)之后的pw.dismiss()将解决问题
@Override
protected void onPostExecute(PopupWindow pw) {
// TODO Auto-generated method stub
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
pw.dismiss();
}