当我尝试将标签添加到我的Android应用程序时显示错误

时间:2014-10-09 08:19:17

标签: android c#-4.0 xamarin

我在编译代码时不断收到此错误错误CS0120:非静态字段,方法或属性需要对象引用' Android.Widget.TabHost.NewTabSpec(string)'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace EmployeeDirectory.Android
{
    [Activity (Label = "TabsLayoutActivity" , MainLauncher = true, Icon="@drawable/ic_launcher")]           
    public class TabsLayoutActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.TabsLayout);
            CreateTab(typeof(CalendarActivity), "Calendar", "Calendar",      Resource.Drawable.Calendar);
            CreateTab(typeof(HospitalActivity), "Nearest Hospital", "Nearest Hospital", Resource.Drawable.Hospital);
            CreateTab(typeof(MapActivity), "Maps and Location", "Maps and Location", Resource.Drawable.Maps);
            CreateTab(typeof(EverciseActivity), "Exercises", "Exercises", Resource.Drawable.Exercises);
            CreateTab(typeof(DietActivity), "Diet", "Diet", Resource.Drawable.Diet);
            // Create your application here
        }
        private void CreateTab(Type activityType, string tag, string label, int drawableId )
        {
            var intent = new Intent(this, activityType);
            intent.AddFlags(ActivityFlags.NewTask);

            var spec = TabHost.NewTabSpec(tag);
            var drawableIcon = Resources.GetDrawable(drawableId);
            spec.SetIndicator(label, drawableIcon);
            spec.SetContent(intent);

            TabHost.AddTab(spec);
    }
}

}

1 个答案:

答案 0 :(得分:0)

正如Selvin指出的那样,你应该获得对你的TabHost的引用,然后调用它的(实例)方法NewTabSpec,如:

TabHost th = FindViewById<TabHost>(Resource.Id.myTabHost);
var spec = th.NewTabSpec(tag);