Quantcast

tabbed interface with fragment

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

tabbed interface with fragment

Wally McClure
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
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: tabbed interface with fragment

Jonathan Pryor-2
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
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: tabbed interface with fragment

Wally McClure
Thanks Jon.  I was able to get this working.  Smile
 
Wally
> From: [hidden email]

> Date: Wed, 30 Nov 2011 14:07:35 -0500
> To: [hidden email]
> Subject: Re: [mono-android] tabbed interface with fragment
>
> 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
> > [hidden email]
> >
> > UNSUBSCRIBE INFORMATION:
> > http://lists.ximian.com/mailman/listinfo/monodroid
>
> _______________________________________________
> Monodroid mailing list
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Loading...