Bug 32736 - I add a Activity with create by 'LoadApplication(application)' to TabHost, then I get a 'System.NullReferenceException'.
Summary: I add a Activity with create by 'LoadApplication(application)' to TabHost, th...
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: unspecified
Hardware: All All
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-08-04 04:10 UTC by changyu
Modified: 2015-11-30 15:38 UTC (History)
6 users (show)

Is this bug a regression?: ---
Last known good build:

the demo project (4.34 MB, application/x-rar)
2015-08-04 04:12 UTC, changyu

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description changyu 2015-08-04 04:10:50 UTC
+++ This bug was initially created as a clone of Bug #32555 +++

I want to use a 'TabActivity', and use some 'Forms Page' as its child Activity.
I create a 'TabActivity', and add some child 'Activity' with Create by 'LoadApplication(application)'.
Then I get a exception like this:
------------------------Exception START-----------------------------------
System.NullReferenceException: Object reference not set to an instance of an object
  at Xamarin.Forms.Platform.Android.Platform.UpdateActionBarBackgroundColor () [0x00010] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.Platform.set_CurrentNavigationPage (Xamarin.Forms.NavigationPage value) [0x000f1] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.Platform.UpdateActionBar () [0x00068] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.Platform.SetPage (Xamarin.Forms.Page newRoot) [0x000bf] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.FormsApplicationActivity.InternalSetPage (Xamarin.Forms.Page page) [0x000b7] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.FormsApplicationActivity.SetMainPage () [0x0000c] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.FormsApplicationActivity.LoadApplication (Xamarin.Forms.Application application) [0x0002d] in <filename unknown>:0 
  at App_Tabbed.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00011] in c:\Users\admin\Desktop\App_Tabbed\App_Tabbed\App_Tabbed.Droid\MainActivity.cs:24 
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) [0x00011] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:2707 
  at (wrapper dynamic-method) object:c33eda0c-fc1f-4527-afd2-7fdd4a53b699 (intptr,intptr,intptr)
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in <filename unknown>:0 
  at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue* parms) [0x00063] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:571 
  at Android.Widget.TabHost.AddTab (Android.Widget.TabSpec tabSpec) [0x0005a] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.TabHost.cs:943 
  at App_Tabbed.Droid.MainTab.AddSpec () [0x00001] in c:\Users\admin\Desktop\App_Tabbed\App_Tabbed\App_Tabbed.Droid\MainTab.cs:52 
  at App_Tabbed.Droid.MainTab.OnCreate (Android.OS.Bundle bundle) [0x0003f] in c:\Users\admin\Desktop\App_Tabbed\App_Tabbed\App_Tabbed.Droid\MainTab.cs:35
------------------------Exception END-----------------------------------

My primary code like this:
------------------------Code Start-------------------------
    public class App : Application
        public App()
            MainPage = new NavigationPage(new ScrollPage());

    [Activity(Label = "MainActivity")]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
        protected override void OnCreate(Bundle bundle)

            global::Xamarin.Forms.Forms.Init(this, bundle);

            LoadApplication(new App());

[Activity(Label = "App_Tabbed", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainTab : TabActivity, Android.Widget.RadioGroup.IOnCheckedChangeListener 

        private TabHost tabHost;

        protected override void OnCreate(Bundle bundle)

                tabHost = TabHost;
                LocalActivityManager groupActivity = new LocalActivityManager(this, false);
            catch (Exception e)
                var msg = e.Message;

        private void AddSpec() 
            tabHost.AddTab(this.BuildTagSpec("tab_home", "首页", Resource.Drawable.icon, new Intent(this, typeof(MainActivity))));
            tabHost.AddTab(this.BuildTagSpec("tab_shop", "店铺", Resource.Drawable.icon, new Intent(this, typeof(MainActivity))));
            tabHost.AddTab(this.BuildTagSpec("tab_message", "消息", Resource.Drawable.icon, new Intent(this, typeof(MainActivity))));
            tabHost.AddTab(this.BuildTagSpec("tab_more", "更多", Resource.Drawable.icon, new Intent(this, typeof(MainActivity))));

        private TabHost.TabSpec BuildTagSpec(String tagName, int tagLable, int icon, Intent content)
            return tabHost.NewTabSpec(tagName)
                .SetIndicator(Resources.GetString(tagLable), Resources.GetDrawable(icon))
        private TabHost.TabSpec BuildTagSpec(String tagName, string tagLable, int icon, Intent content)
            return tabHost.NewTabSpec(tagName)
                .SetIndicator(tagLable, Resources.GetDrawable(icon))

------------------------Code End---------------------------

The bug troubled me a long time. TabbedPage of Forms is too hard to build a beautiful UI. So I have to adopt the top solution.
Any idea?
Comment 1 changyu 2015-08-04 04:12:47 UTC
Created attachment 12347 [details]
the demo project
Comment 2 Rajneesh Kumar 2015-08-06 07:29:56 UTC
I have checked this issue and able to reproduce this. To reproduce this issue I have used the attached test sample provided in bug description. As I Deploy the application on simulator it throws System.NullReferenceException. Please refer the screencast given below.

Screencast: http://www.screencast.com/t/4iUXsfdQ

Application Output: https://gist.github.com/Rajneesh360Logica/0a45aa8ff37ec860dde4
Build Output: https://gist.github.com/Rajneesh360Logica/9cb3b8110ba89239c577
Ide Logs: https://gist.github.com/Rajneesh360Logica/3656b6958cb8c7cf33af
XAP Logs: https://gist.github.com/Rajneesh360Logica/e7487ae295bcf701536e

Environment Info:

XF Version:

=== Xamarin Studio ===

Version 5.9.5 (build 9)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
	Mono 4.0.3 ((detached/d6946b4)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400030020

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/MM/Desktop/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 6.2 (6776)
Build 6C131e

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 2c66d2f
Branch: master
Build date: 2015-08-04 13:52:25-0400

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 509050009
Git revision: b99cb19a1d3b15d8980c4616f13f837457e5b292
Build date: 2015-07-28 02:01:44-04
Xamarin addins: d53d6eceb828422f3d4aff8547f2f6bab6c9ea58

=== Operating System ===

Mac OS X 10.9.5
Darwin MacMini.local 13.4.0 Darwin Kernel Version 13.4.0
    Sun Aug 17 19:50:11 PDT 2014
    root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64
Comment 3 Rui Marinho 2015-11-30 15:37:39 UTC
Using the new FormsAppCompatActivity now allows this to work. 

Add a theme to your styles like this:
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="windowNoTitle">true</item>
  <item name="windowActionBar">false</item>

Set the theme in your activity  Theme="@style/MyTheme" , and change FormsApplicationActivity to FormsAppCompatActivity
Comment 4 Rui Marinho 2015-11-30 15:38:08 UTC
Oh and make sure to update to XF 2.0