Adrian Grigore
Adrian Grigore

Reputation: 33318

Monodroid Tab Layout sample project does not seem to work

I'm struggling with the Tab Layout example shown at Xamarin's website. I've created the state-list drawable xml files and copied the icons to my drawable directory as described there, but I have trouble with the OnCreate method.

The OnCreate method they are listing is obviously broken as it is missing a TabHost instantiation. But even when fixing this with a call to

var TabHost = new TabHost(this);

I still get a null reference exception. This is the complete source code of OnCreate until the line where it throws:

 protected override void OnCreate(Bundle bundle)
        {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        var TabHost = new TabHost(this);
        TabHost.TabSpec spec;     // Resusable TabSpec for each tab
        Intent intent;            // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent(this, typeof(StopWatchActivity));
        intent.AddFlags(ActivityFlags.NewTask);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = TabHost.NewTabSpec("Stoppuhr");
        spec.SetIndicator("Artists", Resources.GetDrawable(Resource.Drawable.ic_tab_artists_grey));
        spec.SetContent(intent);
        //Crashes with a null reference exception
        TabHost.AddTab(spec);
        ...
}

Why does TabHost.AttTab crash my application with a null reference exception?

As an alternative, if you where I can download a fully working sample project showing the tab layout in action with Monodroid, I'll gladly use that as a reference.

Upvotes: 3

Views: 2255

Answers (1)

chrisntr
chrisntr

Reputation: 2208

There's an example of this working on the APIDemo in the MonoDroid Samples on Xamarin's GitHub: https://github.com/xamarin/monodroid-samples/blob/master/ApiDemo/Tutorials/TabLayoutTutorial.cs

Is your activity subclassing TabActivity? (the

var TabHost = new TabHost(this);

isn't needed at all).

I hope this helps,

ChrisNTR

Upvotes: 3

Related Questions