【问题标题】:System.NullReferenceException during OnElementChanged() in CustomMapRenderer在 CustomMapRenderer 中的 OnElementChanged() 期间发生 System.NullReferenceException
【发布时间】:2017-03-27 09:44:49
【问题描述】:

我正在使用 Xamarin 编写应用程序。该应用程序的一部分包括显示跟踪点的地图。一切都运行良好,除非应用程序在使用 Android 6.0.1 的小米 Redmi 4A 上运行。

当我尝试运行 OnElementChanged(ElementChangedEventArgs e) 时,我收到 System.NullReferenceException: Object reference not set to an instance of an object。

我的代码:

 protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            _customMap = (CustomMap)e.NewElement;
            _unitViewModel = (UnitViewModel)_customMap.BindingContext;
            ((MapView)Control).GetMapAsync(this);

            _unitViewModel.TrackingPositions.CollectionChanged += TrackingPositions_CollectionChanged;
        }
    }

在 base.OnElementChanged(e) 处引发异常。如果这部分包含在 try catch 中,则会在 ((MapView)Control).GetMapAsync(this); 处引发相同的异常;

【问题讨论】:

  • 您的设备是否安装了 google play 服务?某些设备默认不附带它,具体取决于 Android 提供商。设备上未安装 Google 地图将无法使用。
  • 正如SuavePirate所说,请确保您的设备已安装google play服务。如果安装后问题仍然存在,请将您的自定义地图代码发布到 PCL 中。
  • 我有一个 if (CheckPlayService()) 在我开始之前检查,甚至在注册 GCM 之前。我还打开了谷歌地图,它工作得很好。我还发现该应用程序在三星 S4、Wileyfox Swift、三星 J3(?)上运行良好,仅举几例。问题似乎仅限于小米。
  • CustomMap.cs 中的代码:using System.ComponentModel; using Xamarin.Forms.Maps; namespace Celmax { public class CustomMap : Map, INotifyPropertyChanged { } }
  • 我的问题通过在 android 清单中启用 android:hardwareAccelerated="true" 解决了。小米似乎有点挣扎

标签: android google-maps xamarin xamarin.android


【解决方案1】:

通过在 Android Manifest 文件中启用 android:hardwareAccelerated="true" 解决了我的问题。这可能有助于解决一系列设备上的几个相同性质的 NullPointerException。

<application android:label="MyApp" android:icon="@drawable/Logo" android:theme="@style/MyTheme" android:hardwareAccelerated="true">

解决方案首次发布位置的链接:android.content.res.Resources.getConfiguration()' on a null object reference。这家伙是真正的 MVP :D

【讨论】:

    猜你喜欢
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多