raberana
raberana

Reputation: 11816

Null reference exception in Android Xamarin (VS 2012)

Im just trying out Andorid Xamarin in VS 2012. Im doing their Hello World tutorial but I encountered a problem. I have declared my main layout resources in this way:

enter image description here

In my activity, whenever i try to find the resource through their id, it just returns null.

enter image description here

Both aButton and aLabel are null. I dont know what is the cause of this problem. Can you help me please? Thanks.

PS: I am attaching also my resource designer class. My objects are declared there (wink).

enter image description here

EDIT: This is the corrected code. enter image description here

Upvotes: 1

Views: 1379

Answers (1)

Brett Duncavage
Brett Duncavage

Reputation: 2183

You are missing a call to SetContentView in your OnCreate method.

var layout = LayoutInflater.Inflate(Resource.Id.Main);
SetContentView(layout);

The first line creates a new view from the resource Main.axml. The second line sets the content view of the activity so it has something to actually display and hence subsequent calls to FindViewById<T>() will be able to find the views.

Upvotes: 3

Related Questions