无法在Popup中添加clickListener按钮
我在我的Android应用程序中弹出。在弹出窗口中,我有用于关闭弹出窗口id =“imbClosePopUp”的按钮,但是当我尝试设置该按钮时,clickListener应用程序崩溃。无法在Popup中添加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>
我设置为onclicklistener在我创建弹出式功能按钮imbClosePopUp(我创建点击按钮的一些弹出窗口)。
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,如何colse popup?
假设我明白这个问题好了,我怀疑问题是在这里:
ImageButton imbClosePopUp=(ImageButton)findViewById(R.id.imbClosePopUp);
这是Activity.findViewById()
你打电话那里,对不对?但你大概想要搜索弹出的布局,而不是活动的。试试这个:
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);
}
这工作,感谢一下! – Damir 2010-11-07 20:45:10
试试这个
imbClosePopUp.setOnClickListener(新View.OnClickListner(){
finish();
});
再次崩溃,同样发生了 – Damir 2010-11-07 20:09:29
很好,我们需要看到你的堆栈跟踪。其次,这看起来像是一个非常糟糕的实现,无论你在做什么。你为什么不只是使用对话框? – Falmarri 2010-11-07 19:55:32
对话框和弹出窗口有什么不同? – Damir 2010-11-07 20:10:00