简体   繁体   中英

Xamarin.IOS MvvmCross App throws NullReference Exception in ViewDidLoad method in iOS9

I am working on an existing Xamarin iOS application with a PCL and uses MvvmCross for IoC and Databinding. Built on MvvmCross 3.1.1

The App looks like a TabControl App but it is buttons on the MainViewCotnroller giving the Tab feel to it. Views are declared in the Storyboard and in the MainViewController ,

PrepareForSegue(UIStoryboardSegue segue, NSObject sender) method

is overridden to load the selected "Tab" View based on the Segue selected - like below

  if (segue.Identifier == "FirstSegue")
 {
     var dest = segue.DestinationViewController as FirstView;
     dest.Request = new MvxViewModelRequest<FirstViewModel>(null, null, new MvxRequestedBy());
     foreach (UIView v in src.placeHolderView.Subviews)
     {
         v.RemoveFromSuperview();
     }
     dest.View.Frame = new CGRect(0f, 0f, w, h);
     src.placeHolderView.Add(dest.View);
 }

The App runs fine on iPhones with iOS 7.1 till 8.4.

Just before iOS 9 was released I did a test installation for the App on iPhone 6 with iOS 9.1 beta and the App crashed on loading. I retrieved the device logs (below) and analysed that the problem is in the guts of the MvvmCross ViewDidLoad method. I updated the latest SDKs (XCode 7, Xamarin.IOS 9.0) and followed instructions provided by Xamarin and compiled the App again using the latest SDKs same error. Updated the Nuget package reference for MvvmCross to 3.5.1 and still the same issue

The issues is there is a null reference at Line 44 in the 3.5 MvvMCross Source Code in GitHub

Note : This piece of code has not changed since 3.1 at least and 3.5.1 is the latest in the nuget for MvvmCross

Stack Trace below (App Names, Project Names changed for security reasons):

   2015-09-21 11:06:26.991 MyApp[1210:60b] Unhandled managed exception:
Object reference not set to an instance of an object (System.NullReferenceException)
  at Cirrious.CrossCore.Mvx.Resolve[IMvxViewModelLoader] () [0x00006] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods.LoadViewModel (IMvxTouchView touchView) [0x00057] in <filename unknown>:0 
  at Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate (IMvxView view, System.Func`1 viewModelLoader) [0x00012] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods.OnViewCreate (IMvxTouchView touchView) [0x00001] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerAdapter.HandleViewDidLoadCalled (System.Object sender, System.EventArgs e) [0x00007] in <filename unknown>:0 
  at (wrapper delegate-invoke) <Module>:invoke_void_object_EventArgs (object,System.EventArgs)
  at Cirrious.CrossCore.Core.MvxDelegateExtensionMethods.Raise (System.EventH
andler eventHandler, System.Object sender) [0x00004] in <filename unknown>:0 
  at Cirrious.CrossCore.Touch.Views.MvxEventSourceTableViewController.ViewDidLoad () [0x00006] in <filename unknown>:0 
  at MyProject.Touch.MostPurchasedView.ViewDidLoad () [0x00048] in d:\Working\Repo\MyApp\<MyProjectName>\MyProject.Touch\Views\OneOfTheView.cs:31 
  at (wrapper managed-to-native) ObjCRuntime.Messaging:IntPtr_objc_msgSendSuper (intptr,intptr)
  at UIKit.UIViewController.get_View () [0x00030] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/build/ios/native/UIKit/UIViewController.g.cs:2632 
  at MyProject.Touch.HomeViewController.PrepareForSegue (UIKit.UIStoryboardSegue segue, Foundation.NSObject sender) [0x0016a] in d:\Working\Repo\MyApp\MyProject.Mobile\MyProject.Touch\HomeViewController.cs:335 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at 
UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/UIKit/UIApplication.cs:74 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/UIKit/UIApplication.cs:57 
  at MyProject.Touch.Application.Main (System.String[] args) [0x00001] in d:\Working\Repo\MyApp\MyProject.Mobile\MyProject.Touch\Main.cs:16 
2015-09-21 11:06:26.994 MyApp[1210:60b] critical: Stacktrace:
Native stacktrace:
.....

I tried the above with just calling the below in the View.cs:

    public override void ViewDidLoad()
    {
        try
        {
            base.ViewDidLoad();
        }
        catch (Exception ex)
        {
           // throw a toast msg in the UI            
        }
    }

I disabled all the base.ViewDidLoad() in all the View.cs files in the ViewDidLoad override methods to take it to the next level and the same problem but inside the constructor of MvxSimpleTableViewSource class

Device Log below:

Object reference not set to an instance of an object (System.NullReferenceException)
      at Cirrious.CrossCore.Mvx.TryResolve[IMvxTouchSystem] (IMvxTouchSystem& service) [0x00006] in <filename unknown>:0 
      at Cirrious.CrossCore.Touch.Platform.MvxIosMajorVersionChecker.ReadIsIosVersionOrHigher (Int32 target, Boolean defaultValue) [0x00000] in <filename unknown>:0 
      at Cirrious.CrossCore.Touch.Platform.MvxIosMajorVersionChecker..ctor (Int32 major, Boolean defaultValue) [0x00006] in <filename unknown>:0 
      at Cirrious.MvvmCross.Binding.Touch.Views.MvxSimpleTableViewSource..ctor (UIKit.UITableView tableView, System.String nibName, System.String cellIdentifier, Foundation.NSBundle bundle) [0x00000] in <filename unknown>:0 
      at MyProject.Touch.MostPurchasedView.ViewDidLoad () [0x00059] in d:\Working\Repo\MyApp\MyProject.Mobile\MyProject.Touch\Views\OneOfTheView.cs:42 
      at (wrapper managed-to-native) ObjCRuntime.Messaging:IntPtr_objc_msgSendSuper (intptr,intptr)
      at UIKit.UIViewController.get_View () [0x00030] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/build/ios/native/UIKit/UIViewController.g.cs:2632 
      at MyProject.Touch.HomeViewController.PrepareForSegue (UIKit.UIStoryboardSegue segue, Foundation.NSObject sender) [0x0016a] in d:\Working\Repo\MyApp\MyProject.Mobile\MyProject.Touch\HomeViewController.cs:343 
      at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
      at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/UIKit/UIApplication.cs:74 
      at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/UIKit/UIApplication.cs:57 
      at MyProject.Touch.Application.Main (System.String[] args) [0x00001] in d:\Working\Repo\MyApp\MyProject.Mobile\MyProject.Touch\Main.cs:16 

In short :

Only in iOS 9.1, MvvmCross 3.1.1 AND MvvmCross 3.5.1 has a NullReference Issue in line 44 of the MvxViewControllerExtensionMethods class which gets called during ViewDidLoad but works well with iOS 8.4 and below

I am just getting familiar with MvvmCross framework and unsure whether it is an issue with MvvmCross and iOS 9.

Has anyone come across this issue? Any help on this would be appreciated.

Note : The MvvmCross samples work fine with iOS9. So I believe it has to be something with the Tab implementation.

One thing is that MvvmCross 3.1.1 not is build for iOS unified code. This will prevent you from submitting your app. MvvmCross 4.0 is currently in beta, but i would recommend using it, because it has many fixes.

If the code crashes on line 44 while trying to resolve IMvxViewModelLoader , it probably means that you are missing some package where the IMvxViewModelLoader is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM