1

I don't know why it is not working but what am I doing wrong. It works fine if I explicitly set the TextView and not using Inflate method by the LayoutInflater. It just closes my application when I run it and when I switch to Test tab it'll just crash with no errors in the output window.

public class MyProfileActivity : Activity
{
      protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        ActionBar actionBar = ActionBar;
        actionBar.NavigationMode = ActionBarNavigationMode.Tabs;
        actionBar.SetDisplayShowTitleEnabled (false);

        ActionBar.Tab tab = actionBar.NewTab ();
        tab.SetText ("Home");
       // tab.SetIcon (Resource.Drawable.tab_home);
        tab.SetTabListener(new TabListener<HomeFragment>(this, "home"));
        actionBar.AddTab (tab);

        tab = actionBar.NewTab ();
        tab.SetText("Terms");
       // tab.SetIcon(Resource.Drawable.tab_terms);
        tab.SetTabListener (new TabListener<TermsFragment> (this, "terms"));
        actionBar.AddTab (tab);


        tab = actionBar.NewTab();
        tab.SetText("test");
        // tab.SetIcon(Resource.Drawable.tab_terms);
        tab.SetTabListener(new TabListener<TestFragment>(this, "test"));
        actionBar.AddTab(tab);
    }
}

Fragments

    public class TestFragment : Fragment
    {
        private View _fragmentView;

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

            _fragmentView = inflater.Inflate(Resource.Layout.Tabs, container);
            return _fragmentView;
        }
    }

#region HomeFragment
public class HomeFragment : Fragment
{
    public HomeFragment() : base() {}

    public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        TextView tv = new TextView (Activity);
        tv.Text = "Home";

        return tv;
    }
}
#endregion

#region TermsFragment
public class TermsFragment : Fragment
{
    public TermsFragment() : base() {}

    public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        TextView tv = new TextView (Activity);
        tv.Text = "Terms";

        return tv;
    }
}
#endregion

TabListener

   public class TabListener<T> : Java.Lang.Object, ActionBar.ITabListener where T : Fragment
    {
        #region Members
        private Fragment m_fragment;
        private readonly Activity m_activity;
        private readonly string m_tag;
        private readonly T m_fragmentType;
        #endregion

        #region Constructor
        public TabListener(Activity activity, String tag)
        {

                // The Activity is our host Activity and provides the Context.
                // The type T and the tag will be used to instantiate the fragment.
                // m_fragment will hold the instance for reuse after it is first created.
                m_activity = activity;
                m_tag = tag;
            }
            #endregion

            #region ITabListener implementation
            public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
            {
                // Instantiate the reference and add it to the container, 
                // or attach it if it has already been created
                if (m_fragment == null)
                {
                    m_fragment = Fragment.Instantiate(m_activity, Java.Lang.Class.FromType(typeof(T)).Name);
                    ft.Add(global::Android.Resource.Id.Content, m_fragment, m_tag);
                }
                else
                {
                    ft.Show(m_fragment);
                }
            }

            public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
            {
                if (m_fragment != null)
                {
                    ft.Hide(m_fragment);
                }
            }

            public void OnTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
            {
                // User selected the already selected tab. Usually do nothing.
            }
            #endregion

       public class TestFragment : Fragment
        {
            private View _fragmentView;

            public override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);

                // Create your fragment here
            }

            public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {

                _fragmentView = inflater.Inflate(Resource.Layout.Tabs, container);
                return _fragmentView;
            }
        }
     }
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
  • How does it fail. Please post a Stack Trace and a better description of what you are trying. Just dumping all your code does not benefit anyone. – Cheesebaron Sep 23 '13 at 17:23
  • Can you read? You still have the possibility to get information through logcat! – Cheesebaron Sep 23 '13 at 20:05
  • 1
    I'm sorry, I'll include something. Can you my next comment? It's under Visual Studio 2012 with Xamarin plugin. I don't have logcat because I am not using android studio. Xamarin is aware of the debugging experience in VS. – 123 456 789 0 Sep 23 '13 at 20:15

1 Answers1

0

I tried using with your code and got java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. when navigating to TestFragment.

This answer allowed me to fix the problem by providing false for parameter attachToRoot:

_fragmentView = inflater.Inflate(Resource.Layout.TestLayout, container, false);
Community
  • 1
  • 1
rpggio
  • 2,447
  • 1
  • 19
  • 13