我有:
popUp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
....
<Button
android:id="@+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:gravity="left"
/>
</LinearLayout>
main.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
popUp();
}
});
我强行关闭
public void popUp(){
LayoutInflater inflater = (LayoutInflater) project.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup,null, false),300,400,true);
ok_button = (Button) findViewById(R.id.ok_button);
pw.showAtLocation(findViewById(R.id.list), Gravity.BOTTOM, 0,10);
ok_button.setOnClickListener(new OnClickListener() { //here I get FC
@Override
public void onClick(View v) {
pw.dismiss();
}
});
}
}
当我从main.xml而不是popUp.xml使用按钮(在popUp();中)时,一切正常。 使用非main.xml中的按钮有什么问题
答案 0 :(得分:2)
我认为问题在于:
ok_button = (Button) findViewById(R.id.ok_button);
它正在寻找主要布局内的按钮视图,而不是在弹出布局中查找。所以ok_button可能是null。
尝试从构造函数中移出inflate调用,如下所示:
View v = inflater.inflate(R.layout.popup,null, false);
final PopupWindow pw = new PopupWindow(v,300,400,true);
然后:
ok_button = (Button) v.findViewById(R.id.ok_button);