无法在弹出窗口中为按钮添加clickListener

我的Android应用程序中有弹出窗口。在弹出窗口中,我有关闭弹出窗口的按钮,id =“imbClos​​ePopUp”,但是当我尝试设置为该按钮时,clickListener应用程序崩溃。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:background="#AAAAAAAA">

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:layout_gravity="right"
    >
    <ImageButton
    android:id="@+id/imbClosePopUp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:src="@drawable/close"
    ></ImageButton>
    </LinearLayout>
    <TextView
        android:text="CEKIRAJ OBAVEZNE KRITERIJUME"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </TextView> 
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <TableRow>
            <CheckBox
                android:id="@+id/chkw"
                android:checked="true"
                android:text="cg"
            ></CheckBox>
            <CheckBox
                android:id="@+id/chkq"
                android:checked="true"
                android:text="tv"
            ></CheckBox>
        </TableRow>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_gravity="right"
>
        <Button
            android:id="@+id/btnSaveCrit"
            android:text="ok"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
        >
        </Button>
    </LinearLayout>
</LinearLayout>
我在创建Popup的函数中设置onclicklistener for button imbClos​​ePopUp(我点击一些按钮创建Popup)。
  private OnClickListener AdditionalC = new OnClickListener() {
        public void onClick(View v) {
            LayoutInflater inflater = (LayoutInflater)
            roomate.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          pw = new PopupWindow(
            inflater.inflate(R.layout.popup, null, false), 
            300, 
            300, 
            true);
         // The code below assumes that the root container has an id called 'main'
         pw.showAtLocation(roomate.findViewById(R.id.tbC), Gravity.CENTER, 0, 0); 

         ImageButton imbClosePopUp=(ImageButton)findViewById(R.id.imbClosePopUp);
         imbClosePopUp.setOnClickListener(ClosePopUp);


        }
    };
有人知道什么是ptoblem以及如何收集弹出窗口?     
已邀请:
假设我理解这个问题,我怀疑问题出在这里:
ImageButton imbClosePopUp=(ImageButton)findViewById(R.id.imbClosePopUp);
那是你在那里打电话的,对吧?但是你可能想要搜索弹出窗口的布局,而不是活动的布局。试试这个:
public void onClick(View v) {
    LayoutInflater inflater = (LayoutInflater)
    roomate.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView=inflater.inflate(R.layout.popup, null, false);
    pw = new PopupWindow(popupView, 300, 300, true);
    pw.showAtLocation(roomate.findViewById(R.id.tbC), Gravity.CENTER, 0, 0); 

    ImageButton imbClosePopUp=(ImageButton)popupView.findViewById(R.id.imbClosePopUp);
    imbClosePopUp.setOnClickListener(ClosePopUp);
}
    
试试这个 imbClos​​ePopUp.setOnClickListener(new View.OnClickListner() {
finish();
});     

要回复问题请先登录注册