KaiEn Suizai
KaiEn Suizai

Reputation: 79

Customize ActionBar cause Tabs unable to work

Here i need a Customize Action Bar and Tab Fragment in my Xamarin Android project. Because of i have the Customize Action Bar and I need to close the original ActionBar, so this is my manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pitsapp1x.pitsapp1x" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="16" />
    <application android:theme="@android:style/Theme.NoTitleBar"></application>
</manifest>

and now on my tab, it need ActionBar.NavigationMode

public class frmTab : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.frmTab);

HERE>>>     this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

            AddTab("Buy", Resource.Drawable.Icon, new frmTabBuy());
            AddTab("Home", Resource.Drawable.Icon, new frmTabHome());
        }

        private void AddTab(string tabText, int iconResourceId, Fragment fragment)
        {
 HERE>>>    var tab = this.ActionBar.NewTab();
            tab.SetText(tabText);
            tab.SetIcon(iconResourceId);

            tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
            {
               e.FragmentTransaction.Replace(Resource.Id.fragmentContainer, fragment);
            };

 HERE>>>    this.ActionBar.AddTab(tab);
    }

It cause the result keep return me

System.NullReferenceException: Object reference not set to an instance of an object

Any solution for me to link to my Customize ActionBar or don't use the Action Bar?? thanks for help...

Upvotes: 0

Views: 724

Answers (1)

Artem Zelinskiy
Artem Zelinskiy

Reputation: 2210

As i see from your code, you are using toolbar as custom action bar. If this is true, then you need to set it as your action bar

var toolbar = FindViewById<Toolbar>("Your toolbar id");
SetSupportActionBar(toolbar); 

If you do not use toolbar then you need to remove

@android:style/Theme.NoTitleBar

Upvotes: 1

Related Questions