FragmentTabHost in VS2013 - xamarin C#
-
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
-
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
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 );
-
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 );
-
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?
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. -
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. -
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!
-
Then you need to establish what value is causing the exception. We cannot guess it from the information you have provided.
-
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.
-
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.
-
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.
-
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!