2014-10-09 110 views
-1

我不断收到此错误,我编译代码错误CS0120:对象引用是非静态字段,方法或属性'Android.Widget.TabHost .NewTabSpec(串)”错误,当我尝试添加标签到我的android应用程序

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); 
    } 
} 

}

+0

* *对象引用是r适用于非静态**字段,**方法**或属性**'Android.Widget.TabHost.NewTabSpec(string)'** – Selvin 2014-10-09 08:24:49

回答

0

由于Selvin指出,你应该得到你的TabHost的引用,然后调用它(实例)方法NewTabSpec,如:

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

我应该在哪里添加代码? – Monster 2014-10-09 08:54:10

+0

第一行我会放入OnCreate,然后将它作为参数传递给CreateTab。 – 2014-10-09 08:54:47

+0

如果你不介意困惑,请在代码中显示我 – Monster 2014-10-09 09:01:45

相关问题