【问题标题】:Customize ActionBar cause Tabs unable to work自定义 ActionBar 导致 Tabs 无法工作
【发布时间】:2016-03-14 23:07:31
【问题描述】:

我需要在我的 Xamarin Android 项目中自定义操作栏和选项卡片段。 因为我有自定义操作栏,我需要关闭原来的操作栏,所以这是我的清单:

<?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>

现在在我的选项卡上,它需要 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);
    }

导致结果一直返回给我

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

我有什么解决方案可以链接到我的自定义操作栏或不使用操作栏吗?感谢您的帮助...

【问题讨论】:

    标签: android xamarin tabs android-actionbar


    【解决方案1】:

    正如我从您的代码中看到的,您使用工具栏作为自定义操作栏。如果这是真的,那么您需要将其设置为您的操作栏

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

    如果你不使用工具栏那么你需要删除

    @android:style/Theme.NoTitleBar
    

    【讨论】:

    • 如果它不是工具栏,你知道吗?顺便说一句,我只找到了 SetActionBar(Toolbar toolbar)
    • 是一样的。如果您正在使用支持库并且您正在使用 SetActionBar 时您不需要它,则使用“支持”版本。我建议你使用工具栏来自定义动作栏developer.android.com/reference/android/widget/Toolbar.html如果你不想使用它,那么你不需要关闭原来的动作栏。你只需要修改它
    猜你喜欢
    • 2012-11-16
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多