{"action":"created","issue":{"url":"https://api.github.com/repos/MvvmCross/MvvmCross/issues/1112","labels_url":"https://api.github.com/repos/MvvmCross/MvvmCross/issues/1112/labels{/name}","comments_url":"https://api.github.com/repos/MvvmCross/MvvmCross/issues/1112/comments","events_url":"https://api.github.com/repos/MvvmCross/MvvmCross/issues/1112/events","html_url":"https://github.com/MvvmCross/MvvmCross/issues/1112","id":102893861,"number":1112,"title":"AppCompatActivity and ActionBarActivity cause null reference exception onCreate(Bundle ....)","user":{"login":"thetempest25","id":11541907,"avatar_url":"https://avatars.githubusercontent.com/u/11541907?v=3","gravatar_id":"","url":"https://api.github.com/users/thetempest25","html_url":"https://github.com/thetempest25","followers_url":"https://api.github.com/users/thetempest25/followers","following_url":"https://api.github.com/users/thetempest25/following{/other_user}","gists_url":"https://api.github.com/users/thetempest25/gists{/gist_id}","starred_url":"https://api.github.com/users/thetempest25/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thetempest25/subscriptions","organizations_url":"https://api.github.com/users/thetempest25/orgs","repos_url":"https://api.github.com/users/thetempest25/repos","events_url":"https://api.github.com/users/thetempest25/events{/privacy}","received_events_url":"https://api.github.com/users/thetempest25/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":12,"created_at":"2015-08-24T21:18:40Z","updated_at":"2015-10-12T07:51:18Z","closed_at":null,"body":"Hello,\r\n\r\n<b>Update</b>\r\nI have recreated this issue in a test repo, No custom code, all packages are latest version and from nuget:\r\nhttps://github.com/thetempest25/AppCompatTest\r\n\r\n< TLDR >\r\nTried MVVMCross 3.5.2-alpha2 and 4.0.0.0-alpha2, no help, same results.\r\n\r\nThe problem:\r\nMVVMCross, using the MVVMCross Android Support Library, is not fully loaded when Android displays MainLauncher Activity when using AppCompatActivity or ActionBarActivity as the class root; i.e. android OS tires to display ViewModel before MVVMCross is fully loaded.\r\n\r\nThe solution:\r\nMake Activity (from Xamarin Android) the root for all Activies, however I can't do this because I lose my support for actionbar and material themeing.\r\n\r\nQuestions:\r\n1. Any known solutions or possible suggestions?\r\n<i>2. How does MVVMCross ensure that it's loaded before Android OS displays a given View (e.g. 1 - how is MvxApplication.Setup(...) called? && 2 - How does anroidOS wait for MVVMCross to finish loading?)</i>\r\n\r\n< Full Version >\r\n\r\nI have an odd problem.  I am using MVX 3.5.2-alpha2, Android 4.4 is the target, and I am trying to use AppCompat to get Material Themes into KitKat.  I was using the splash screen activity, and everything was working okay, until I noticed that sometimes, the program would not resume properly from the background.\r\n\r\nWhen the program would resume, one of three things would happen:\r\n1) The program would restart from scratch, e.g. splash screen etc...\r\n2) The program would resume correctly to the proper activity with all information available\r\n3) The program would crash and continually crash, the stacktrace shows that it crashes in Android.Activity.onCreate(Bundle ....)\r\n\r\nThe crash was coming from the IoC container trying to resolve a BindingStack<T> (no my splash screen doesn't have any bindings; I am aware of that issue).  The problem is that the Container (IoC) was null (e.g. the MVVMCross framework hadn't loaded completely yet...).\r\n\r\nI was able to get the crash to happen all of the time by removing the splash screen as the Main Launcher and just using my LoginView as the main launcher.\r\n\r\nFuture testing shows that the problem occurs when MvxActionBarActivity/EventSource inherit either AppCompat (v7) or ActionBarActivity (v7).  If I make the base class \"Activity\" everything works properly, of course my actionbar and my themes stop working, but that's to be expected.\r\n\r\nI am not sure why the activity isn't waiting for the MVVMCross framework to load when AppCompat/ActionBarActivity are the base class, and yet it forces the Framework to load when \"Activity\" is the base class.\r\n\r\nHere is the stack trace for the error:\r\n\r\n```\r\n08-24 17:15:31.513 I/MonoDroid( 2894): UNHANDLED EXCEPTION:\r\n08-24 17:15:31.523 I/MonoDroid( 2894): System.NullReferenceException: Object reference not set to an instance of an object\r\n08-24 17:15:31.523 I/MonoDroid( 2894): at Cirrious.CrossCore.Mvx.Resolve<Cirrious.MvvmCross.Binding.BindingContext.IMvxBindingContextStack`1<Cirrious.MvvmCross.Binding.Droid.BindingContext.IMvxAndroidBindingContext>> () <0x00068>\r\n08-24 17:15:31.523 I/MonoDroid( 2894): at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContextHelpers.Current<Cirrious.MvvmCross.Binding.Droid.BindingContext.IMvxAndroidBindingContext> () <0x0003f>\r\n08-24 17:15:31.533 I/MonoDroid( 2894): at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContextHelpers.Current () [0x00001] in C:\\Users\\bmd\\Documents\\Visual Studio 2015\\Projects\\MvvmCross-3.5(1)\\MvvmCross-3.5\\Cirrious\\Cirrious.MvvmCross.Binding.Droid\\BindingContext\\MvxAndroidBindingContextHelpers.cs:17\r\n08-24 17:15:31.533 I/MonoDroid( 2894): at Cirrious.MvvmCross.Binding.Droid.Views.MvxLayoutInflater.Inflate (int,Android.Views.ViewGroup,bool) [0x00017] in C:\\Users\\bmd\\Documents\\Visual Studio 2015\\Projects\\MvvmCross-3.5(1)\\MvvmCross-3.5\\Cirrious\\Cirrious.MvvmCross.Binding.Droid\\Views\\MvxLayoutInflater.cs:109\r\n08-24 17:15:31.533 I/MonoDroid( 2894): at Android.Views.LayoutInflater.n_Inflate_ILandroid_view_ViewGroup_Z (intptr,intptr,int,intptr,bool) [0x00011] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.LayoutInflater.cs:667\r\n08-24 17:15:31.533 I/MonoDroid( 2894): at (wrapper dynamic-method) object.e87cccea-9d6a-4898-9855-fba4c75e836f (intptr,intptr,int,intptr,bool) <IL 0x00023, 0x00047>\r\n08-24 17:15:31.543 I/MonoDroid( 2894): UNHANDLED EXCEPTION:\r\n08-24 17:15:31.543 I/MonoDroid( 2894): System.NullReferenceException: Object reference not set to an instance of an object\r\n08-24 17:15:31.543 I/MonoDroid( 2894): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x0004b>\r\n08-24 17:15:31.543 I/MonoDroid( 2894): at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue*) [0x00084] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1029\r\n08-24 17:15:31.543 I/MonoDroid( 2894): at Android.App.Activity.OnCreate (Android.OS.Bundle) [0x00070] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:2725\r\n08-24 17:15:31.553 I/MonoDroid( 2894): at MvxActionBarEventSourceActivity.OnCreate (Android.OS.Bundle) [0x0000f] in C:\\Users\\bmd\\Documents\\Visual Studio 2015\\Projects\\LaredoPolice\\Antares.LaredoPolice.Core\\Antares.LaredoPolice.UI.Droid\\mvvmcross\\MvxActionBarEventSourceActivity.cs:16\r\n08-24 17:15:31.553 I/MonoDroid( 2894): at Antares.LaredoPolice.UI.Droid.Views.LoginView.OnCreate (Android.OS.Bundle) [0x00001] in C:\\Users\\bmd\\Documents\\Visual Studio 2015\\Projects\\LaredoPolice\\Antares.LaredoPolice.Core\\Antares.LaredoPolice.UI.Droid\\Views\\LoginView.cs:33\r\n08-24 17:15:31.553 I/MonoDroid( 2894): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:2707\r\n08-24 17:15:31.553 I/MonoDroid( 2894): at (wrapper dynamic-method) object.39236647-7bcf-408d-b0f6-8adffa889a18 (intptr,intptr,intptr) <IL 0x00017, 0x00027>\r\n08-24 17:15:31.553 D/AndroidRuntime( 2894): Shutting down VM\r\n08-24 17:15:31.553 W/dalvikvm( 2894): threadid=1: thread exiting with uncaught exception (group=0xb1cc7b20)\r\nAn unhandled exception occured.\r\n```\r\n\r\nHere is the code I am using for the Activity and Event Source:\r\n\r\n<b>MvxActionBarActivity</b>\r\n```\r\npublic class MvxActionBarActivity\r\n    : MvxActionBarEventSourceActivity\r\n    , IMvxAndroidView\r\n{\r\n    public MvxActionBarActivity()\r\n    {\r\n        BindingContext = new MvxAndroidBindingContext(this, this);\r\n        this.AddEventListeners();\r\n    }\r\n\r\n    public object DataContext\r\n    {\r\n        get { return BindingContext.DataContext; }\r\n        set { BindingContext.DataContext = value; }\r\n    }\r\n\r\n    public IMvxViewModel ViewModel\r\n    {\r\n        get { return DataContext as IMvxViewModel; }\r\n        set\r\n        {\r\n            DataContext = value;\r\n            OnViewModelSet();\r\n        }\r\n    }\r\n    public void MvxInternalStartActivityForResult(Intent intent, int requestCode)\r\n    {\r\n        base.StartActivityForResult(intent, requestCode);\r\n    }\r\n\r\n    public IMvxBindingContext BindingContext { get; set; }\r\n\r\n    public override void SetContentView(int layoutResId)\r\n    {\r\n        var view = this.BindingInflate(layoutResId, null);\r\n        SetContentView(view);\r\n    }\r\n\r\n    protected virtual void OnViewModelSet()\r\n    {\r\n    }\r\n\r\n    protected override void AttachBaseContext(Context @base)\r\n    {\r\n        if (this is IMvxAndroidSplashScreenActivity)\r\n        {\r\n            // Do not attach our inflater to splash screens.\r\n            base.AttachBaseContext(@base);\r\n            return;\r\n        }\r\n        base.AttachBaseContext(MvxContextWrapper.Wrap(@base, this));\r\n    }\r\n}\r\n```\r\n\r\n<b>MvxActionBarActivityEventSource</b>\r\n```\r\npublic abstract class MvxActionBarEventSourceActivity\r\n    : AppCompatActivity //ActionBarActivity  //AppCompatActivity \r\n    , IMvxEventSourceActivity\r\n{\r\n    protected override void OnCreate(Bundle bundle)\r\n    {\r\n            CreateWillBeCalled.Raise(this, bundle);\r\n        base.OnCreate(bundle);\r\n        CreateCalled.Raise(this, bundle);\r\n    }\r\n\r\n    protected override void OnDestroy()\r\n    {\r\n        DestroyCalled.Raise(this);\r\n        base.OnDestroy();\r\n    }\r\n\r\n    protected override void OnNewIntent(Intent intent)\r\n    {\r\n        base.OnNewIntent(intent);\r\n        NewIntentCalled.Raise(this, intent);\r\n    }\r\n\r\n    protected override void OnResume()\r\n    {\r\n        base.OnResume();\r\n        ResumeCalled.Raise(this);\r\n    }\r\n\r\n    protected override void OnPause()\r\n    {\r\n        PauseCalled.Raise(this);\r\n        base.OnPause();\r\n    }\r\n\r\n    protected override void OnStart()\r\n    {\r\n        base.OnStart();\r\n        StartCalled.Raise(this);\r\n    }\r\n\r\n    protected override void OnRestart()\r\n    {\r\n        base.OnRestart();\r\n        RestartCalled.Raise(this);\r\n    }\r\n\r\n    protected override void OnStop()\r\n    {\r\n        StopCalled.Raise(this);\r\n        base.OnStop();\r\n    }\r\n\r\n    public override void StartActivityForResult(Intent intent, int requestCode)\r\n    {\r\n        StartActivityForResultCalled.Raise(this, new MvxStartActivityForResultParameters(intent, requestCode));\r\n        base.StartActivityForResult(intent, requestCode);\r\n    }\r\n\r\n    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)\r\n    {\r\n        ActivityResultCalled.Raise(this, new MvxActivityResultParameters(requestCode, resultCode, data));\r\n        base.OnActivityResult(requestCode, resultCode, data);\r\n    }\r\n\r\n    protected override void OnSaveInstanceState(Bundle outState)\r\n    {\r\n        SaveInstanceStateCalled.Raise(this, outState);\r\n        base.OnSaveInstanceState(outState);\r\n    }\r\n\r\n    protected override void Dispose(bool disposing)\r\n    {\r\n        if (disposing)\r\n        {\r\n            DisposeCalled.Raise(this);\r\n        }\r\n        base.Dispose(disposing);\r\n    }\r\n\r\n    public event EventHandler DisposeCalled;\r\n    public event EventHandler<MvxValueEventArgs<Bundle>> CreateWillBeCalled;\r\n    public event EventHandler<MvxValueEventArgs<Bundle>> CreateCalled;\r\n    public event EventHandler DestroyCalled;\r\n    public event EventHandler<MvxValueEventArgs<Intent>> NewIntentCalled;\r\n    public event EventHandler ResumeCalled;\r\n    public event EventHandler PauseCalled;\r\n    public event EventHandler StartCalled;\r\n    public event EventHandler RestartCalled;\r\n    public event EventHandler StopCalled;\r\n    public event EventHandler<MvxValueEventArgs<Bundle>> SaveInstanceStateCalled;\r\n    public event EventHandler<MvxValueEventArgs<MvxStartActivityForResultParameters>> StartActivityForResultCalled;\r\n    public event EventHandler<MvxValueEventArgs<MvxActivityResultParameters>> ActivityResultCalled;\r\n}\r\n```\r\n\r\n\r\nQuestions:\r\n1. Any known solutions or possible suggestions?\r\n<i>2. How does MVVMCross ensure that it's loaded before Android OS displays a given View (e.g. 1 - how is MvxApplication.Setup(...) called? && 2 - How does anroidOS wait for MVVMCross to finish loading?)</i>\r\n\r\nThank you in advance"},"comment":{"url":"https://api.github.com/repos/MvvmCross/MvvmCross/issues/comments/147317791","html_url":"https://github.com/MvvmCross/MvvmCross/issues/1112#issuecomment-147317791","issue_url":"https://api.github.com/repos/MvvmCross/MvvmCross/issues/1112","id":147317791,"user":{"login":"Cheesebaron","id":249719,"avatar_url":"https://avatars.githubusercontent.com/u/249719?v=3","gravatar_id":"","url":"https://api.github.com/users/Cheesebaron","html_url":"https://github.com/Cheesebaron","followers_url":"https://api.github.com/users/Cheesebaron/followers","following_url":"https://api.github.com/users/Cheesebaron/following{/other_user}","gists_url":"https://api.github.com/users/Cheesebaron/gists{/gist_id}","starred_url":"https://api.github.com/users/Cheesebaron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Cheesebaron/subscriptions","organizations_url":"https://api.github.com/users/Cheesebaron/orgs","repos_url":"https://api.github.com/users/Cheesebaron/repos","events_url":"https://api.github.com/users/Cheesebaron/events{/privacy}","received_events_url":"https://api.github.com/users/Cheesebaron/received_events","type":"User","site_admin":false},"created_at":"2015-10-12T07:51:18Z","updated_at":"2015-10-12T07:51:18Z","body":"The problem is that you are not using the SplashScreen which MvvmCross usually provides. So when you launch your own activity the MvvmCross subsystem has not initialized. That is why you are getting that NPE."}}