0

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.

datachat
  • 51
  • 7
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mohit S Nov 25 '15 at 03:16
  • dialogbutton is likely null, because you are calling FindViewById inside the click handler, and it is using the context of the Main view, not the dialog view, so it cannot find the button. – Jason Nov 25 '15 at 04:49
  • How to reference the dialogbutton then? So that I can add a click event to it. – datachat Nov 25 '15 at 05:20
  • @datachat because you need to refernce on your dialog. Check my answer! – XTL Nov 25 '15 at 09:36
  • @Vetalio Thank you so much. – datachat Nov 25 '15 at 16:37
  • @datachat you're welcome ;)! – XTL Nov 25 '15 at 17:16

1 Answers1

2

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!

XTL
  • 1,452
  • 2
  • 17
  • 40