1

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

3
  • Can you post more of you activity's OnCreate method? Commented Apr 7, 2013 at 4:11
  • ok.. just a minute sir. Commented Apr 7, 2013 at 4:13
  • I edited my post, updated the activity class image above to show my whole oncreate method. Commented Apr 7, 2013 at 4:15

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ow... i get it now... i declared aButton and aLabel as a child of Main Layout. I cannot use them in other layout (i created a new layout in the oncreate method). Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.