我做了一个小应用程序,我用 TYPE_SYSTEM_OVERLAY 加载一个新视图,现在我想点击一个按钮关闭它,我收到了错误:
Activity com.example.viewandbutton.View2Class has leaked window android.widget.RelativeLayout@44957168 that was originally added here
我已经搜索了错误并发现我必须dismiss()
该活动但是dismiss仅适用于 Dialog 对象,所以我不知道如何停止活动。
这是我的代码:
public class View2Class extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view2);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.view2, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
final WindowManager wm = (WindowManager) this.getSystemService(WINDOW_SERVICE);
wm.addView(myView, params);
Button btnActivity = (Button)findViewById(R.id.button2);
btnActivity.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
finish();
}
});
}
}
谢谢!
答案 0 :(得分:1)
根据我的意见,在View
之前删除onClick()
按钮finish()
,如下:
btnActivity.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
wm.removeView(myView);
finish();
}
});
答案 1 :(得分:0)
这是完整的工作代码......
public class View2Class extends Activity {
View myView;
WindowManager wm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.view2);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.view2, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT
);
final WindowManager wm = (WindowManager) this.getSystemService(WINDOW_SERVICE);
wm.addView(myView, params);
Button bb = (Button) myView.findViewById(R.id.button2);
bb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(myView);
finish();
}
});
}
}