Henk
Henk

Reputation: 9

Unable to set "Main view" as ContentView in Android project with VS 2015

I'm facing issues with Visual Studio 2015 when developing an Android Application.

For some reason I'm not able to set my Main view in MyActivity

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Id.Main);
    }

When building it returns the error "'Resource.Id' does not contain a definition for 'Main'", although the view is called "Main.axml" I did have this issue before, but could solve that by saving the view, which always helped.

I'm not sure, but it looks like it has something to do with a recent update of Xamarin.

I've tried several things: Clean solution, Rebuild (ends in error), Closing VS and re-open, restart computer. I've also created a new project, but that didn't help either.

Below is my (simple) Main.axml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="read" />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Upvotes: 0

Views: 131

Answers (1)

user5535613
user5535613

Reputation:

It should be:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);
}

because you are referring to a layout you want that activity to look. Not a specific view in a layout.

Upvotes: 1

Related Questions