Android 安卓系统:弹出和按钮,以消除不起作用

Android 安卓系统:弹出和按钮,以消除不起作用,android,button,popup,dismiss,Android,Button,Popup,Dismiss,我有: 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" .... <But

我有:

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.xml而不是popUp.xml使用按钮(在popUp();)时,一切都正常。
使用not main.xml中的按钮有什么问题?我认为问题出在这里:

ok_button = (Button) findViewById(R.id.ok_button);
它在主布局中查找按钮视图,而不是在弹出布局中查找。所以ok_按钮可能是空的

尝试从构造函数中移出充气调用,如下所示:

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);
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);