我在我的 Xamarin 表单应用程序中实现了 Google map ,但在 map 完成加载当前位置之前离开页面时出现上述异常。
这可能与之前提出但未得到解答的问题相同 here .
根据我的研究,我认为这个问题与这个单独问题中给出的泄漏抽象答案有关:MonoDroid: Error when calling constructor of custom view - TwoDScrollView
但是我没有足够的 Java 或 Android 开发知识来知道如何解决这个问题。我希望有人可以解释在发生此异常时我可以在哪里以及如何处理它。基本上我认为我需要实现的是捕获异常并在 Droid 项目中处理它,但是在哪里?
这些是我收到的关键异常消息。
Message: [NotSupportedException: Unable to activate instance of type Xamarin.Forms.Maps.Android.MapRenderer from native handle 0xbef7ad5c (key_handle 0xd4608e7).]
Message: [MissingMethodException: No constructor found for Xamarin.Forms.Maps.Android.MapRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)]
Message: [Exception of type 'Java.Interop.JavaLocationException' was thrown.]
最佳答案
However I do not have enough knowledge of Java or Android development to know how to resolve this issue. I am hoping that someone can explain where and how I can handle this exception when it occurs.
我认为原因是当您浏览页面时,Xamarin.Forms.Maps.Android.MapRenderer
的Dispose
方法在加载其余部分之前被调用的 map 。 ACW
需要创建一个新的MapRenderer
实例,但是创建失败,因为MapRenderer
没有(IntPtr,IntPtr, JniHandleOwnership)
.
如果你引用Premature Dispose() Calls你可以找到以下语句:
If the subclass does contain an (IntPtr, JniHandleOwnership) constructor, then a new instance of the type will be created.
所以这个异常的解决方法是为 Xamarin.Forms.Maps.Android.MapRenderer
创建一个子类(比方说 MyMapRenderer
),它的构造函数有两个参数:(IntPtr, JniHandleOwnership)
,并使用MyMapRenderer
进行 map 渲染:
在 PCL 中为
Xamarin.Forms.Map.Map
创建一个自定义控件,并在您的项目中使用MyMap
:public class MyMap:Map{}
在 Droid 项目中创建一个自定义
MapRenderer
,它有一个(IntPtr, JniHandleOwnership)
构造函数:[assembly:ExportRenderer(typeof(MyMap),typeof(MyMapRenderer))] namespace YourNameSpace.Droid { public class MyMapRenderer:MapRenderer { public MyMapRenderer(IntPtr handle, JniHandleOwnership transfer) { } } }
关于创建自定义MapRenderer
的详细信息,请引用Custom a Map .
关于android - [MissingMethodException:未找到 Xamarin.Forms.Maps.Android.MapRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) 的构造函数],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42254083/