I attempting to create custom tabs using this. But when I try to create an instance of the TextView from the inflated layout and use it as the View in my TabHost.TabSpec, I receive
“Unhandled Exception:
Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.”
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>
If I replace
TextView tv = (TextView)v.FindViewById(Resource.Id.tabsText);
in getView with
TextView tv = new TextView(this);
Then I do not receive that error. So it definitely seems to have something to do with tabsText in tabs_bg even though that is exactly the way the author did it in the example.
The example you are porting from does not return the TextView from createTabView, it returns the LinearLayout. You are returning tv instead of v in getView.