[mono-android] tabbed interface with fragment
Wally McClure
theevilprogrammer at hotmail.com
Mon Nov 21 11:06:44 EST 2011
I'm trying to write a simple example using a tabbed interface in the actionbar. When a user clicks on a tab, a new fragment will load in the main screen of the device. When I click the 2nd tab, I am getting an error. in DDMS, I get an error shown below. I'm not sure if the error is due to frag1, frag2, or something else. Any suggestion are appreciated.
error:
11-21 10:55:40.220: E/AndroidRuntime(3911):
java.lang.IllegalStateException: The specified child already has a
parent. You must call removeView() on the child's parent first.
code:
[Activity(Label = "HCTabbedInterface", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity, ActionBar.ITabListener
{
int count = 1;
private ActionBar ab;
private View mActionBarView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
ab = this.ActionBar;
ab.AddTab(ab.NewTab().SetText(tab1).SetTabListener(this));
ab.AddTab(ab.NewTab().SetText(tab2).SetTabListener(this));
mActionBarView = LayoutInflater.Inflate(
Resource.Layout.ab, null);
ab.CustomView = mActionBarView;
ab.DisplayOptions = ActionBarDisplayOptions.ShowCustom | ActionBarDisplayOptions.UseLogo;
ab.NavigationMode = ActionBarNavigationMode.Tabs;
ab.SetDisplayShowHomeEnabled(true);
}
public void OnTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
}
private const string tab1 = "Tab 1";
private const string tab2 = "Tab 2";
private Frag1 f1;
private Frag2 f2;
public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
try
{
switch (tab.Text)
{
case tab1:
if (f1 == null)
{
f1 = FragmentManager.FindFragmentById<Frag1>(Resource.Id.fg);
}
else
{
ft.Replace(Resource.Id.fg, f1);
}
break;
case tab2:
if (f2 == null)
{
f2 = (Frag2)Fragment.Instantiate(this, (new Frag2()).Class.Name);
}
ft.Replace(Resource.Id.fg, f2);
break;
}
}
catch (System.Exception sysExc)
{
Android.Util.Log.Error("tab", String.Format("Error info: {0}", sysExc.Message));
}
}
public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
{
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/monodroid/attachments/20111121/9d0d5cac/attachment.html
More information about the Monodroid
mailing list