[mono-android] tabbed interface with fragment
Jonathan Pryor
jonp at xamarin.com
Wed Nov 30 14:07:35 EST 2011
Any chance you could file a bug with the project? I don't know what Frag1 or Frag2 are supposed to be, nor do I know what Resource.Layout.ab is supposed to be, etc.
Thanks,
- Jon
On Nov 21, 2011, at 11:06 AM, Wally McClure wrote:
> 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)
> {
> }
> }
>
>
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
More information about the Monodroid
mailing list