我正在尝试弹出窗口的示例并面临2个问题 1)弹出窗口没有关闭 2)showAtLocation正在给出空指针异常。
public void onShowPopup(View v){
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.mainActivity);
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = layoutInflater.inflate(R.layout.informationpopup, relativeLayout);
final PopupWindow popWindow = new PopupWindow(getBaseContext());
popWindow.setContentView(inflatedView);
popWindow.setBackgroundDrawable(new BitmapDrawable());
popWindow.setFocusable(true);
//popWindow.setOutsideTouchable(true);
//popWindow.showAtLocation(inflatedView, Gravity.NO_GRAVITY, 0, 0);
// Getting a reference to Close button, and close the popup when clicked.
Button close = (Button) findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast t = Toast.makeText(getBaseContext(), "Close", Toast.LENGTH_LONG);
t.show();
popWindow.dismiss();
}
});
}
答案 0 :(得分:0)
你膨胀的方式是错误的。 showAtLocation视图也应该是显示弹出窗口的父视图。
在这里,你应该工作....
public void onShowPopup(View v){
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = layoutInflater.inflate(R.layout.informationpopup, null,false);
final PopupWindow popWindow = new PopupWindow(getBaseContext());
popWindow.setContentView(inflatedView);
popWindow.setBackgroundDrawable(new BitmapDrawable());
popWindow.setFocusable(true);
popWindow.setOutsideTouchable(true);
// Getting a reference to Close button, and close the popup when clicked.
Button close = (Button) inflatedView.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast t = Toast.makeText(getBaseContext(), "Close", Toast.LENGTH_LONG);
t.show();
popWindow.dismiss();
}
});
popWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popWindow.showAtLocation(v, Gravity.NO_GRAVITY, v.getLeft(),v.getBottom());
}