0

So I am adding new button to my app but when FindViewById is called it returns null and app crashes. In my .cs file I have following function:

void fooUIButtonHandler()  
{  
    var fooUIButton = FindViewById(Resource.Id.fooUIButton);  
    fooUIButton.Click += delegate  
    {  
        if (!CheckOriginalImage()) 
        { 
            return; 
        } 

        Intent intent = new Intent(this, typeof(fooImageDemoActivity));  
        intent.PutExtra(fooImageDemoActivity.EXTRAS_ARG_IMAGE_FILE_URI,   
            originalImageUri.ToString());  
        StartActivityForResult(intent, REQUEST_SB_FOO_UI);  
    };  
} 

and my .axml file looks like this:

<Button  
    android:id="@+id/fooUIButton"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:background="#0b8442"  
    style="@style/Widget.AppCompat.Button.Borderless"  
    android:text="foo"/>

I am setting Content View in OnCreate:

SetContentView(Resource.Layout.FooDocumentCreation);

and then calling:

fooUIButtonHandler()

But following line is throwing Null exception:

var fooUIButton = FindViewById(Resource.Id.fooUIButton);

Any ideas how to fix it?

Matthew Andrews
  • 437
  • 1
  • 5
  • 16
Ika
  • 201
  • 3
  • 17
  • Check out https://stackoverflow.com/questions/25209916/xamarin-findviewbyid-returns-null?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Kyle May 08 '18 at 21:34
  • Try clean & build. also are you sure the `fooUIButton` is inside `FooDocumentCreation`? – Mehdi Dehghani May 09 '18 at 05:14
  • Also, you can delete the bin and obj folder in your project, and restart your VS. – Robbit May 09 '18 at 05:25

0 Answers0