I have popup in my android app. In popup I have button for closing popup with id=”imbClosePopUp”, but when I try to set to that button clickListener app crashs.
<?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>
I set onclicklistener for button imbClosePopUp in function where I create Popup ( I create Popup on click some button ).
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);
}
};
Does anybody know what is the ptoblem and how to colse popup ?
Assuming I understand the question alright, I suspect the problem is here:
That’s
Activity.findViewById()you’re calling there, right? But you presumably want to be searching the popup‘s layout, not the activity’s. Try this: