0

I am using MVVMCross for my Xamarin Android application. Sometimes when I navigate from one viewmodel to another I get

System.Collections.Generic.KeyNotFoundException has been thrown

This occurs at base.OnCreate(bundle) of the following function when a new activity is being created.

protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            if (contentResourceId >= 0)
            {
                SetContentView(contentResourceId);
            }

            DoOnCreate(bundle);
        }

This can't be reproduced by following certain steps and is very random. I was wondering if anyone else faces this kind of issue and is there any way to resolve it.

UPDATE: ERROR LOG

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
[MonoDroid]   at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x0001e] in <657aa8fea4454dc898a9e5f379c58734>:0 
[MonoDroid]   at MvvmCross.Droid.Views.MvxChildViewModelCache.Get (System.Int32 index) [0x00000] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MonoDroid]   at MvvmCross.Droid.Views.MvxAndroidViewsContainer.TryGetEmbeddedViewModel (Android.Content.Intent intent, MvvmCross.Core.ViewModels.IMvxViewModel& mvxViewModel) [0x0001a] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MonoDroid]   at MvvmCross.Droid.Views.MvxAndroidViewsContainer.Load (Android.Content.Intent intent, MvvmCross.Core.ViewModels.IMvxBundle savedState, System.Type viewModelTypeHint) [0x0005e] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MonoDroid]   at MvvmCross.Droid.Views.MvxActivityViewExtensions.LoadViewModel (MvvmCross.Droid.Views.IMvxAndroidView androidView, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x0006a] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MonoDroid]   at MvvmCross.Droid.Views.MvxActivityViewExtensions+<>c__DisplayClass1_0.<OnViewCreate>b__1 () [0x0000a] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MonoDroid]   at MvvmCross.Core.Views.MvxViewExtensionMethods.OnViewCreate (MvvmCross.Core.Views.IMvxView view, System.Func`1[TResult] viewModelLoader) [0x00012] in <c4031697dde6429f89882cbe7dd9e363>:0 
[MonoDroid]   at MvvmCross.Droid.Views.MvxActivityViewExtensions.OnViewCreate (MvvmCross.Droid.Views.IMvxAndroidView androidView, Android.OS.Bundle bundle) [0x00062] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MonoDroid]   at MvvmCross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled (System.Object sender, MvvmCross.Platform.Core.MvxValueEventArgs`1[T] eventArgs) [0x0000c] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MonoDroid]   at (wrapper delegate-invoke) System.EventHandler`1[MvvmCross.Platform.Core.MvxValueEventArgs`1[Android.OS.Bundle]]:invoke_void_object_TEventArgs (object,MvvmCross.Platform.Core.MvxValueEventArgs`1<Android.OS.Bundle>)
[MonoDroid]   at MvvmCross.Platform.Core.MvxDelegateExtensionMethods.Raise[T] (System.EventHandler`1[TEventArgs] eventHandler, System.Object sender, T value) [0x00000] in D:\git\MvvmCross\MvvmCross\Platform\Platform\Core\MvxDelegateExtensionMethods.cs:21 
[MonoDroid]   at MvvmCross.Droid.Support.V4.EventSource.MvxEventSourceFragmentActivity.OnCreate (Android.OS.Bundle bundle) [0x00014] in <9170bdccdedc4890abe52ca8a0a43540>:0 
[MonoDroid]   at The.Droid.Views.Abstract.BaseSupportActivity`1[T].OnCreate (Android.OS.Bundle bundle) [0x00001] in /Users/ali00261/Loyalty-MultiBanner-Mobile/The.Droid/Views/Abstract/BaseSupportActivity.cs:51 
[MonoDroid]   at The.Droid.Views.HomeView.OnCreate (Android.OS.Bundle bundle) [0x00001] in /Users/ali00261/Loyalty-MultiBanner-Mobile/The.Droid/Views/HomeView.cs:45 
[MonoDroid]   at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00011] in <013f8eacb1954462a2f9d5f677673fee>:0 
[MonoDroid]   at (wrapper dynamic-method) System.Object:3386debb-3ab9-40f4-9ccb-1b1815452f9b (intptr,intptr,intptr)
[MobileCenterCrashes] Unhandled Exception:
[MobileCenterCrashes] System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
[MobileCenterCrashes]   at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x0001e] in <657aa8fea4454dc898a9e5f379c58734>:0 
[MobileCenterCrashes]   at MvvmCross.Droid.Views.MvxChildViewModelCache.Get (System.Int32 index) [0x00000] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MobileCenterCrashes]   at MvvmCross.Droid.Views.MvxAndroidViewsContainer.TryGetEmbeddedViewModel (Android.Content.Intent intent, MvvmCross.Core.ViewModels.IMvxViewModel& mvxViewModel) [0x0001a] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MobileCenterCrashes]   at MvvmCross.Droid.Views.MvxAndroidViewsContainer.Load (Android.Content.Intent intent, MvvmCross.Core.ViewModels.IMvxBundle savedState, System.Type viewModelTypeHint) [0x0005e] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MobileCenterCrashes]   at MvvmCross.Droid.Views.MvxActivityViewExtensions.LoadViewModel (MvvmCross.Droid.Views.IMvxAndroidView androidView, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x0006a] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MobileCenterCrashes]   at MvvmCross.Droid.Views.MvxActivityViewExtensions+<>c__DisplayClass1_0.<OnViewCreate>b__1 () [0x0000a] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MobileCenterCrashes]   at MvvmCross.Core.Views.MvxViewExtensionMethods.OnViewCreate (MvvmCross.Core.Views.IMvxView view, System.Func`1[TResult] viewModelLoader) [0x00012] in <c4031697dde6429f89882cbe7dd9e363>:0 
[MobileCenterCrashes]   at MvvmCross.Droid.Views.MvxActivityViewExtensions.OnViewCreate (MvvmCross.Droid.Views.IMvxAndroidView androidView, Android.OS.Bundle bundle) [0x00062] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MobileCenterCrashes]   at MvvmCross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled (System.Object sender, MvvmCross.Platform.Core.MvxValueEventArgs`1[T] eventArgs) [0x0000c] in <c173f0f9274a4c068a91342649d69b4c>:0 
[MobileCenterCrashes]   at (wrapper delegate-invoke) System.EventHandler`1[MvvmCross.Platform.Core.MvxValueEventArgs`1[Android.OS.Bundle]]:invoke_void_object_TEventArgs (object,MvvmCross.Platform.Core.MvxValueEventArgs`1<Android.OS.Bundle>)
[MobileCenterCrashes]   at MvvmCross.Platform.Core.MvxDelegateExtensionMethods.Raise[T] (System.EventHandler`1[TEventArgs] eventHandler, System.Object sender, T value) [0x00000] in D:\git\MvvmCross\MvvmCross\Platform\Platform\Core\MvxDelegateExtensionMethods.cs:21 
[MobileCenterCrashes]   at MvvmCross.Droid.Support.V4.EventSource.MvxEventSourceFragmentActivity.OnCreate (Android.OS.Bundle bundle) [0x00014] in <9170bdccdedc4890abe52ca8a0a43540>:0 
[MobileCenterCrashes]   at The.Droid.Views.Abstract.BaseSupportActivity`1[T].OnCreate (Android.OS.Bundle bundle) [0x00001] in /Users/ali00261/Loyalty-MultiBanner-Mobile/The.Droid/Views/Abstract/BaseSupportActivity.cs:51 
[MobileCenterCrashes]   at The.Droid.Views.HomeView.OnCreate (Android.OS.Bundle bundle) [0x00001] in /Users/ali00261/Loyalty-MultiBanner-Mobile/The.Droid/Views/HomeView.cs:45 
[MobileCenterCrashes]   at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00011] in <013f8eacb1954462a2f9d5f677673fee>:0 
[MobileCenterCrashes]   at (wrapper dynamic-method) System.Object:3386debb-3ab9-40f4-9ccb-1b1815452f9b (intptr,intptr,intptr)
TheDeveloper
  • 1,127
  • 1
  • 18
  • 55
  • Add the following to your activity attribute - "ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize". Does it help? – Sean Stayns Dec 28 '17 at 18:34
  • Let me try if that does the trick. – TheDeveloper Dec 28 '17 at 18:36
  • What do you think the reason is and why would this help? – TheDeveloper Dec 28 '17 at 18:38
  • You say it comes very randomly. If the screen size or the orientation change, then an exception like you describe can be thrown. – Sean Stayns Dec 28 '17 at 19:00
  • [Here is about Bundle class](https://developer.android.com/reference/android/os/Bundle.html) and [here is about `KeyNotFoundException`](https://stackoverflow.com/questions/16605354/monocross-keynotfoundexception-in-mxdialogactivityview-oncreate-when-re-start). I suggest you to check the `bundle` above `base.OnCreate(bundle)`, or you should check `OnSaveInstanceState` method if you have overided. – Robbit Dec 29 '17 at 06:13
  • Can you please add more code around the OnCreate method? I'm almost sure it could be something in your own base class that fails. Also a stacktrace will be super helpful. – nmilcoff Dec 29 '17 at 13:17
  • @JoeLv Thank you. I will check it out. – TheDeveloper Dec 29 '17 at 15:25
  • @nmilcoff This is my base class. Let me update the crash report. – TheDeveloper Dec 29 '17 at 15:25
  • @nmilcoff I updated the stack trace – TheDeveloper Dec 29 '17 at 20:07
  • What is the base class of the class with this OnCreate() method? Are you extending Activity directly or do you have your own custom base class? – Code-Apprentice Dec 29 '17 at 20:09
  • @Code-Apprentice Its a custom base class for MVVMCross `public abstract class BasetActivity : MvxFragmentActivity where T : MvxViewModel` – TheDeveloper Dec 29 '17 at 20:10

0 Answers0