在我的xamarin.andoird应用程序中获取actionbar null

时间:2018-07-25 11:29:03

标签: android-activity xamarin.android null android-actionbar

我是xamarin.android本机的新手。我正在从本教程中学习和开发此应用程序 Tutorial Video link

但是我的操作栏为null,因此我收到了nullreference异常。教程身份验证者也共享了源代码。

这是我的代码:

MainMenuPage.axml

<?xml version="1.0" encoding="utf-8"?>  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
    android:id="@+id/fragmentContainer"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />
</LinearLayout>

MainActivity.cs

 [Activity(Label = "HotDogMenuActivityWithTabs",Icon = "@drawable/smallicon",Theme = "@style/AppTheme")]
public class HotDogMenuActivityWithTabs : Activity
{
    private ListView hotDogListView;
    private List<HotDog> allHotDogs;
    private HotDogDataService hotDogDataService;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

        AddTab("Favorites", Resource.Drawable.FavoritesIcon, new FavoriteHotDogFragment());
        AddTab("Meat Lovers", Resource.Drawable.MeatLoversIcon, new MeatLoversFragment());
        AddTab("Veggie Lovers", Resource.Drawable.veggieloversicon, new VeggieLoversFragment());
    }

    private void AddTab(string tabText, int iconResourceId, Fragment view)
    {
        var tab = this.ActionBar.NewTab();
        tab.SetText(tabText);
        tab.SetIcon(iconResourceId);//TODO

        tab.TabSelected += delegate (object sender, Android.App.ActionBar.TabEventArgs e)
        {
            var fragment = this.FragmentManager.FindFragmentById(Resource.Id.fragmentContainer);
            if (fragment != null)
                e.FragmentTransaction.Remove(fragment);
            e.FragmentTransaction.Add(Resource.Id.fragmentContainer, view);
        };
        tab.TabUnselected += delegate (object sender, Android.App.ActionBar.TabEventArgs e)
        {
            e.FragmentTransaction.Remove(view);
        };

        this.ActionBar.AddTab(tab);

    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        if (resultCode == Result.Ok && requestCode == 100)
        {
            var selectedHotDog = hotDogDataService.GetHotDogById(data.GetIntExtra("selectedHotDogId", 0));

            var dialog = new Android.App.AlertDialog.Builder(this);
            dialog.SetTitle("Confirmation");
            dialog.SetMessage(string.Format("You've added {0} time(s) the {1}", data.GetIntExtra("amount", 0), selectedHotDog.Name));
            dialog.Show();
        }
    }
}

Style.xaml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" >
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

我在这里做什么错。请帮助 提前tahnkx。

0 个答案:

没有答案