在popwindow里加上spinner会报错,E/AndroidRuntime(6943): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@4670d3b8 is not valid; is your activity running? 原因是当你点击spinner的时候,弹出的窗口找不到它的依附的点,也就是找不到当前activity的上下文,所以会报错。解决方法有多种如下
1,最简便的改进方法就是更改spinner弹出窗口的mode,在xml中设置spinner的spinnerMode为dialog即可,虽然样式很原始,但是是可以解决崩溃的问题的。
private void showPopupWindow() {
//设置contentView
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupwindow, null);
Button mPopbtn = (Button) contentView.findViewById(R.id.button);
Spinner mPopSpinner = (Spinner) contentView.findViewById(R.id.spinner);
//数据
ArrayList data_list = new ArrayList();
data_list.add("一般报警");
data_list.add("图片报警");
data_list.add("视频包括");
data_list.add("设备报警");
//适配器
ArrayAdapter arr_adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, data_list);
//设置样式
//arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//加载适配器
mPopSpinner.setAdapter(arr_adapter);
mPopbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplication(),MainActivity.class));
}
});
mPopWindow = new PopupWindow(contentView,
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);
mPopWindow.setContentView(contentView) ;
//显示PopupWindow
// main.setVisibility(View.INVISIBLE);
View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
mPopWindow.showAtLocation(rootview, Gravity.CENTER,0 , 0);
// 点击其他地方消失
contentView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mPopWindow != null && mPopWindow.isShowing()) {
mPopWindow.dismiss();
mPopWindow = null;
}
return false;
}
});
}
布局文件设置模式就好了如图
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:spinnerMode="dialog"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="50dp">Spinner>
2.可以用popwindowl来的代替spinner,showasdropdown(),
ublic
class
PopWindowSpinnerActivity
extends
Activity {
TextView MyButton;
PopupWindow pw;
ArrayList list;
int
clickPsition = -
1
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_window);
MyButton = (TextView) findViewById(R.id.myButton);
list = getList();
MyButton.setText(list.get(
0
));
MyButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
View myView = getLayoutInflater().inflate(R.layout.pop,
null
);
pw =
new
PopupWindow(myView,
240
,
300
,
true
);
pw.setBackgroundDrawable(getResources().getDrawable(
R.drawable.diaolog_bg));
pw.setFocusable(
true
);
pw.showAsDropDown(MyButton);
ListView lv = (ListView) myView.findViewById(R.id.lv_pop);
lv.setAdapter(
new
ListViewAdapter(PopWindowSpinnerActivity.
this
, list));
lv.setOnItemClickListener(
new
AdapterView.OnItemClickListener() {
@Override
public
void
onItemClick(AdapterView> parent, View view,
int
position,
long
id) {
MyButton.setText(list.get(position));
if
(clickPsition != position) {
clickPsition = position;
}
pw.dismiss();
}
});
}
});
}
/**
* 得到list集合的方法
* @return
*/
public
ArrayList getList() {
ArrayList list =
new
ArrayList();
list.add("一般报警");
list.add("图片报警");
list.add("视频包括");
list.add("设备报警")
return
list;
}
}