datachat
datachat

Reputation: 61

Button inside custom dialog returning null reference exception

I have a custom dialog setup and inside it is a button that I would like to put functionality into, like going to the next Activity or in this case dismiss the dialog. However when I click it its returning a Null reference exception.

Here is my code so far:

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image" />
</RelativeLayout>

Activity code:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);


        SetContentView(Resource.Layout.Main);

        Button button = FindViewById<Button>(Resource.Id.MyButton);

        button.Click += delegate {

            Dialog dialog = new Dialog(this);
            dialog.SetContentView(Resource.Layout.dialog);
            dialog.SetTitle("Titolo");

            Button dialogbutton = FindViewById<Button>(Resource.Id.dialogButtonOK);

            dialogbutton.Click += delegate
            {
                dialog.Dismiss();
            };

            dialog.Show();

        };
    }
}

I have properly referenced the button inside my dialog by using Button dialogbutton = FindViewById<Button>(Resource.Id.dialogButtonOK); but its throwing a Null Reference Exception. Im new to Android programming and I would be grateful if anyone could be kind enough to point out what I am doing wrong here.

Upvotes: 0

Views: 315

Answers (1)

XTL
XTL

Reputation: 1452

You can try to do this:

Button dialogbutton = dialog.FindViewById<Button>(Resource.Id.dialogButtonOK);

Or you can use standart(default dialog) Check my example:

//make your dialog as global
Dialog alertDialog;

//your OnCreate() method
protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);


        SetContentView(Resource.Layout.Main);

        Button button = FindViewById<Button>(Resource.Id.MyButton);
        //now when you click on the button,dialog will appear with button OK(if you click on OK,dialog will disappear)
        button.Click += delegate {

            var builder = new AlertDialog.Builder(_context);
                        builder.SetTitle("Titolo");

                        builder.SetMessage("Test this example");
                        builder.SetCancelable(false);

                        builder.SetPositiveButton("OK", new EventHandler<DialogClickEventArgs>((sender1, e2) =>
                            { 
                                alertDialog.Dismiss();
                            }));
                        alertDialog = builder.Create();
                        alertDialog.SetCanceledOnTouchOutside(false);
                        alertDialog.Show();

        };
    } 

Enjoy!

Upvotes: 2

Related Questions