向Actionbar添加选项卡时,对象引用未设置为对象的实例

时间:2018-05-23 17:25:29

标签: android xamarin.android

我正在尝试将标签添加到 ActionBar 。这是我的 mainActivity

[Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/ic_launcher")]
public class MainActivity : Activity
{
    Fragment[] _fragments;
    protected override void OnCreate(Bundle bundle)
    {
        try
        {
            RequestWindowFeature(WindowFeatures.ActionBar);
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.activity_main);

            ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
            SetContentView(Resource.Layout.activity_main);
            Models.TableModel model = new Models.TableModel();
            List<Tables> tables = model.loadTables();
            _fragments = new Fragment[]
                         {
                         new AllTablesFragment(tables),
                         new ReservedFragment(tables.Where(a=>a.is_reserved==true).ToList()),
                         new VacantFragment(tables.Where(a=>a.is_reserved==false).ToList())
                         };

            AddTabToActionBar(Resource.String.all_tables, Resource.Drawable.tables);
            AddTabToActionBar(Resource.String.reserved_tables, Resource.Drawable.reserved);
            AddTabToActionBar(Resource.String.vacant_tables, Resource.Drawable.Vacant);

        }
        catch (System.Exception ex)
        {
            new AlertDialog.Builder(this)
                  .SetPositiveButton("OK", (sender, args) =>
                   {
                       // User pressed yes
                   })
                  .SetMessage(ex.Message)
                  .SetTitle("Error")
                  .Show();
        }
    }

    void AddTabToActionBar(int labelResourceId, int iconResourceId)
    {
        ActionBar.Tab tab = ActionBar.NewTab()
                                     .SetText(labelResourceId);
        tab.TabSelected += TabOnTabSelected;
        ActionBar.AddTab(tab);
    }

    void TabOnTabSelected(object sender, ActionBar.TabEventArgs tabEventArgs)
    {
        ActionBar.Tab tab = (ActionBar.Tab)sender;
        Fragment frag = _fragments[tab.Position];
        tabEventArgs.FragmentTransaction.Replace(Resource.Id.frameLayout1, frag);
    }
}

我的 activity_main.axml

<?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"
    android:minHeight="25px">
    <FrameLayout
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frameLayout1" />
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lvTables" />
</LinearLayout>

我在上面说了错误:

ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

OnCreate 方法的一行。 我尝试从方法中删除RequestWindowFeature(WindowFeatures.ActionBar);。但结果是一样的。

我看到类似的问题和接受的答案,但我无法得到问题的原因。谁可以帮我这个事。我被困在这里好几个小时。提前谢谢!

0 个答案:

没有答案