Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. FragmentTabHost in VS2013 - xamarin C#

FragmentTabHost in VS2013 - xamarin C#

Scheduled Pinned Locked Moved Android
csharpandroidmobilelinqhelp
14 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jojoba20
    wrote on last edited by
    #1

    I am using FragmentTabHost in xamarin C# and i get error in : mTabHost.AddTab(spec); and the whole project is :(Part that you may need) Activity 2.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.Content.PM;
    using Android.Support.V4.App;

    namespace ANRestaurant.Forms
    {
    [Activity(Label = "ANRestaurant", MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]

    public class Activity2 : FragmentActivity
    {
        FragmentTabHost mTabHost;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
    
            SetContentView(Resource.Layout.activity\_main);
            try
            {
                mTabHost = FindViewById(Resource.Id.tabhostN  );
                Intent intent;
                intent = new Intent(this, typeof(Fragment1));
                intent.AddFlags(ActivityFlags.NewTask);
                //mTabHost.Setup(this, SupportFragmentManager, Resource.Id.tabFrameLayout);
    
    
                TabHost.TabSpec spec;
                spec = mTabHost.NewTabSpec("artists");
                spec.SetIndicator("Artists", Resources.GetDrawable(Resource.Drawable.tab\_icon1)  );
                spec.SetContent(intent);
                mTabHost.AddTab(spec);   **`======>Here the error occered!`** 
                //mTabHost.AddTab(
                //        mTabHost.NewTabSpec("tab2").SetIndicator("Tab 2", null),
                //       intent, null);
                //mTabHost.addTab(
                //        mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
                //        FragmentTab.class, null);
                //mTabHost.addTab(
                //        mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
                //        FragmentTab.class, null);
            }
            catch (Exception ex)
            {
    
    
            }
        }
    }
    

    }

    activity_main.axml

    L 1 Reply Last reply
    0
    • J jojoba20

      I am using FragmentTabHost in xamarin C# and i get error in : mTabHost.AddTab(spec); and the whole project is :(Part that you may need) Activity 2.cs

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      using Android.App;
      using Android.Content;
      using Android.OS;
      using Android.Runtime;
      using Android.Views;
      using Android.Widget;
      using Android.Content.PM;
      using Android.Support.V4.App;

      namespace ANRestaurant.Forms
      {
      [Activity(Label = "ANRestaurant", MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]

      public class Activity2 : FragmentActivity
      {
          FragmentTabHost mTabHost;
          protected override void OnCreate(Bundle bundle)
          {
              base.OnCreate(bundle);
      
      
              SetContentView(Resource.Layout.activity\_main);
              try
              {
                  mTabHost = FindViewById(Resource.Id.tabhostN  );
                  Intent intent;
                  intent = new Intent(this, typeof(Fragment1));
                  intent.AddFlags(ActivityFlags.NewTask);
                  //mTabHost.Setup(this, SupportFragmentManager, Resource.Id.tabFrameLayout);
      
      
                  TabHost.TabSpec spec;
                  spec = mTabHost.NewTabSpec("artists");
                  spec.SetIndicator("Artists", Resources.GetDrawable(Resource.Drawable.tab\_icon1)  );
                  spec.SetContent(intent);
                  mTabHost.AddTab(spec);   **`======>Here the error occered!`** 
                  //mTabHost.AddTab(
                  //        mTabHost.NewTabSpec("tab2").SetIndicator("Tab 2", null),
                  //       intent, null);
                  //mTabHost.addTab(
                  //        mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
                  //        FragmentTab.class, null);
                  //mTabHost.addTab(
                  //        mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
                  //        FragmentTab.class, null);
              }
              catch (Exception ex)
              {
      
      
              }
          }
      }
      

      }

      activity_main.axml

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      According to my counting, the line you have marked is not line 39, as referenced in the error message. However, I suspect that the variable mTabHost has not been set to point to anything. Are you sure you get a valid reference returned in the following statement?

      mTabHost = FindViewById(Resource.Id.tabhostN );

      J 1 Reply Last reply
      0
      • L Lost User

        According to my counting, the line you have marked is not line 39, as referenced in the error message. However, I suspect that the variable mTabHost has not been set to point to anything. Are you sure you get a valid reference returned in the following statement?

        mTabHost = FindViewById(Resource.Id.tabhostN );

        J Offline
        J Offline
        jojoba20
        wrote on last edited by
        #3

        first of all thanks for ur attention. i have it in "activity_main.axml" :

        And the mTabHost isnot nulll in

        mTabHost = FindViewById(Resource.Id.tabhostN );

        Or Possible to give me and C# xamarin example for doing the same i have done? any further question?

        L 1 Reply Last reply
        0
        • J jojoba20

          first of all thanks for ur attention. i have it in "activity_main.axml" :

          And the mTabHost isnot nulll in

          mTabHost = FindViewById(Resource.Id.tabhostN );

          Or Possible to give me and C# xamarin example for doing the same i have done? any further question?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Did you actually check the value of this variable at run time? It matters not what you write in your source code, the only way to be sure it works is to check it at the time of error. If it is not that variable that causes the error then it must be one of the others. I also notice that you have this code within a try/catch block, but you eat the exception. This is really bad practice and leads to situations just such as this. Exceptions provide useful information, so make proper use of them.

          J 1 Reply Last reply
          0
          • L Lost User

            Did you actually check the value of this variable at run time? It matters not what you write in your source code, the only way to be sure it works is to check it at the time of error. If it is not that variable that causes the error then it must be one of the others. I also notice that you have this code within a try/catch block, but you eat the exception. This is really bad practice and leads to situations just such as this. Exceptions provide useful information, so make proper use of them.

            J Offline
            J Offline
            jojoba20
            wrote on last edited by
            #5

            i have used this for sample for that the catch is not complete. at runtime the mtabHost is not null. i have breakepoint in

            mTabHost.AddTab(spec);

            and the error occured in here. thanks in advanced!

            L 1 Reply Last reply
            0
            • J jojoba20

              i have used this for sample for that the catch is not complete. at runtime the mtabHost is not null. i have breakepoint in

              mTabHost.AddTab(spec);

              and the error occured in here. thanks in advanced!

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Then you need to establish what value is causing the exception. We cannot guess it from the information you have provided.

              J 1 Reply Last reply
              0
              • L Lost User

                Then you need to establish what value is causing the exception. We cannot guess it from the information you have provided.

                J Offline
                J Offline
                jojoba20
                wrote on last edited by
                #7

                may i give ur the whole project for downloading? thanks in advanced!

                L 1 Reply Last reply
                0
                • J jojoba20

                  may i give ur the whole project for downloading? thanks in advanced!

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Sorry, no. You need to do the debugging and provide the required information.

                  J 1 Reply Last reply
                  0
                  • L Lost User

                    Sorry, no. You need to do the debugging and provide the required information.

                    J Offline
                    J Offline
                    jojoba20
                    wrote on last edited by
                    #9

                    thanks but i have done that but cannot help! do y have any sample ?

                    L 1 Reply Last reply
                    0
                    • J jojoba20

                      thanks but i have done that but cannot help! do y have any sample ?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Sorry, I don't use Xamarin, and that is not really the solution; if you find another sample and that one does not work, what then? If you aspire to be a developer then you really need to practice diagnosing problems, even if you need help solving them.

                      J 1 Reply Last reply
                      0
                      • L Lost User

                        Sorry, I don't use Xamarin, and that is not really the solution; if you find another sample and that one does not work, what then? If you aspire to be a developer then you really need to practice diagnosing problems, even if you need help solving them.

                        J Offline
                        J Offline
                        jojoba20
                        wrote on last edited by
                        #11

                        thanks for ur advice But the error is exact there and i dont know how to solve that~

                        L 1 Reply Last reply
                        0
                        • J jojoba20

                          thanks for ur advice But the error is exact there and i dont know how to solve that~

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          I have tried to explain how: you use your debugger to step through the code and look to see what variable is not being set up correctly. You can only solve it by collecting all the information around the time of error.

                          J 1 Reply Last reply
                          0
                          • L Lost User

                            I have tried to explain how: you use your debugger to step through the code and look to see what variable is not being set up correctly. You can only solve it by collecting all the information around the time of error.

                            J Offline
                            J Offline
                            jojoba20
                            wrote on last edited by
                            #13

                            Thanks i have done that! I should change :

                            mTabHost.AddTab(spec);

                            To :

                            mTabHost.Setup(this, SupportFragmentManager, Resource.Id.tabFrameLayout);
                            mTabHost.AddTab(spec, Java.Lang.Class.FromType(typeof(Fragment1)), null);

                            end of story That is all!

                            L 1 Reply Last reply
                            0
                            • J jojoba20

                              Thanks i have done that! I should change :

                              mTabHost.AddTab(spec);

                              To :

                              mTabHost.Setup(this, SupportFragmentManager, Resource.Id.tabFrameLayout);
                              mTabHost.AddTab(spec, Java.Lang.Class.FromType(typeof(Fragment1)), null);

                              end of story That is all!

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              Yes, it always helps to read the documentation.

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups