I have created a custom alert dialog using the following code -
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog, null))
.setTitle("test")
.setCancelable(true);
AlertDialog alert11 = builder.create();
alert11.show();
Here is the code of the layout dialog.xml which is used in the alert dialog -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set"/>
</LinearLayout>
Now, how to get a reference of the button to set a click listener?
I tried this -
Button mButton = (Button) findViewById(R.id.button1);
but I get an exception -
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
Is there any other way to access the button?