I had a customized dialog and I put some button on it, the XML file of the dialog is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="350sp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<FrameLayout
android:id="@+id/frameLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirm PSD"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText3"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:inputType="textPassword" />
</FrameLayout>
<FrameLayout
android:id="@+id/frameLayout5"
android:layout_width="254dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:text="OK"/>
<Button
android:id="@+id/button2"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Cancel" />
</FrameLayout>
and this is my code:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Wify Setting");
dialog.show();
It displayed just like what I want, but when I set OnClick(Method name) property in the button1 it doesn’t work and the app crashed, even if I use findViewById(R.id.button1) to find the button and set OnClickListener on it, it still will not work and crash.
Why is that?
use dialog.findViewById(R.id.button1) .
It will work.
Thanks.