Failed to create an instance of the native type 'NSObject'

冷暖自知 提交于 2019-12-10 19:15:45

问题


I'm getting this error on executing the code below

failed to create an instance of native type 'NSObject' it is possible to ignore this condition by setting Class.ThrowOnInitFailure to false

    var alert=new UIAlertView("Error","Something missing !"
                              ,new UIAlertViewDelegate(),"OK",null);

I get this error only for the latest iOS 6 simulator. For my iOS 5 device it works as expected.


回答1:


Try this:

var alert = new UIAlertView ("Error", "Something missing !", null, "OK");



回答2:


This is because of a recent modification to Xamarin.iOS.

Traditionally MonoTouch has allowed managed objects to be created without a native peer. The behavior has however been inconsistent between types, and in the case of types from third-party libraries it would result in instances which would, if used, most likely crash the process with a stack overflow. So the default behavior has been changed: an exception will be thrown if a native peer cannot be created for a managed object. As the exception message also mentions this behavior is controlled by the value MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure - set it to false to return to the old behavior.

In your case you get the exception because you're trying to create a UIAlertViewDelegate instance when there is no corresponding Objective-C class (since UIAlertViewDelegate is an Objective-C protocol, not an Objective-C class).

The solution is simple, as user1010710 already mentioned, just use null instead of new UIAlertViewDelegate(). The result is identical: in previous versions of Xamarin.iOS you would end up with a managed UIAlertViewDelegate instance whose Handle is IntPtr.Zero - and that IntPtr.Zero value would be passed to the native constructor. This is exactly what happens when you pass null.



来源:https://stackoverflow.com/questions/15522371/failed-to-create-an-instance-of-the-native-type-nsobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!