2012-03-26 6 views
1

thisを使用してカスタムタブを作成しようとしています。カスタムタブ「指定した子には既に親があります」

Java.Lang.IllegalStateException:指定された子私は膨張したレイアウトからのTextViewのインスタンスを作成して、私のTabHost.TabSpecで見るようにそれを使用しようとする。しかし、私は

」未処理の例外を受け取ります既に親を持っています。最初に子の親に対して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> 

私は

TextView tv = new TextView(this); 
でgetViewメソッドで

TextView tv = (TextView)v.FindViewById(Resource.Id.tabsText); 

を交換した場合

それから私はそのエラーを受け取りません。だから間違いなく、たとえ作者がその例で行ったのと同じ方法であっても、tabs_begのtabsTextと何か関係があるようです。

答えて

3

あなたはcreateTabViewからのTextViewを返さないから移植されている例は、のLinearLayoutを返します。 getTVの代わりに、を返しています。

+0

それはそれでした。ありがとう! – jmease

0

使用

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

同じ例外です。 – jmease

関連する問題