Android 自定义选项卡";指定的子级已具有父级;

Android 自定义选项卡";指定的子级已具有父级;,android,xamarin.android,Android,Xamarin.android,我正在尝试使用创建自定义选项卡。但是,当我尝试从膨胀的布局创建TextView实例并将其用作我的TabHost.TabSpec中的视图时,我收到 “未处理的异常: Java.Lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView() Main.cs public class Main : TabActivity { protected override void OnCreate(Bundle bundle) {

我正在尝试使用创建自定义选项卡。但是,当我尝试从膨胀的布局创建TextView实例并将其用作我的TabHost.TabSpec中的视图时,我收到

“未处理的异常:

Java.Lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()

Main.cs

public class Main : TabActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);

        Intent[] intents = new Intent[3];
        intents[0] = new Intent(this, typeof(Inspection)).PutExtra("Name", "Inspection");
        intents[1] = new Intent(this, typeof(Transfer)).PutExtra("Name", "Transfer");
        intents[2] = new Intent(this, typeof(ServiceCalls)).PutExtra("Name", "Service Calls");

        foreach (var intent in intents)
        {
            intent.AddFlags(ActivityFlags.NewTask);
            TextView tv = getView(intent.GetStringExtra("Name"));
            TabHost.TabSpec spec = TabHost.NewTabSpec(intent.GetStringExtra("Name")).SetIndicator(tv).SetContent(intent);
            TabHost.AddTab(spec);
        }

        TabHost.CurrentTab = 0;
    }

    private TextView getView(string text)
    {
        View v = LayoutInflater.Inflate(Resource.Layout.tabs_bg, (ViewGroup)FindViewById(Android.Resource.Id.Content));            
        TextView tv = (TextView)v.FindViewById(Resource.Id.tabsText);
        tv.Text = text;

        return tv;
    }
}
tabs_bg.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tabsLayout" android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical">
   <TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:background ="@drawable/tab_selector"
    android:textSize="18dip"/>
</LinearLayout>
在getView中与

TextView tv = new TextView(this);
那么我没有收到那个错误。因此,它显然与tabs_bg中的tabsText有关,尽管这正是作者在示例中所做的

使用

View v = LayoutInflater.Inflate(Resource.Layout.tabs_bg, null); 

您正在从中移植的示例不会从createTabView返回文本视图,而是返回线性布局。您返回的是tv,而不是getView中的v

View v = LayoutInflater.Inflate(Resource.Layout.tabs_bg, null);