SupportMapFragment 地图为空

2024-05-02

我使用以下代码在 Xamarin.Android 中显示地图:

private SupportMapFragment mapFragment;
private GoogleMap map;

protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.Main);

            mapFragment = SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;
            if (mapFragment == null) {
                GoogleMapOptions options = new GoogleMapOptions ()
                    .InvokeCompassEnabled (true)
                    .InvokeMapType (GoogleMap.MapTypeNone)
                    .InvokeZoomControlsEnabled (false);

                FragmentTransaction frx = SupportFragmentManager.BeginTransaction ();
                mapFragment = SupportMapFragment.NewInstance (options);
                frx.Add (Resource.Id.map,mapFragment,"map");
                frx.Commit ();
            }


if (map == null)
                map = mapFragment.Map;

            CircleOptions circle = new CircleOptions ();
            circle.InvokeCenter (new LatLng(18.5203,73.8567));
            circle.InvokeRadius (1000);
            map.AddCircle (circle);

我的 AXML 是

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

在设备上成功部署后,在线出现异常map.AddCircle (circle); as System.NullReferenceException已被抛出Object Reference is not set to an instance of an Object.

我究竟做错了什么?是不是有什么东西需要初始化?


这行:

SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;

当您尝试投射时不应该起作用View来到外面Mono.Android。因此你必须使用:

SupportFragmentManager.FindFragmentByTag<SupportMapFragment>("map");

or

SupportFragmentManager.FindFragmentByTag ("map").JavaCast<SupportMapFragment>();

您还试图通过以下方式找到片段Tag。然而,你并没有给出你的Fragment a Tag在你的XML中,所以你需要通过以下方式找到它Id反而:

var mapFragment = SupportFragmentManager.FindFragmentById<SupportMapFragment>(Resource.Id.map);

实际上,从您粘贴的代码中可以看出:

SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;

将始终为空,因为您实际上完全忽略了布局,并且您将始终使用手动创建的SupportMapFragment in the if (mapFragment == null)健康)状况...

另外,上次我检查时,FindFragmentByXXX不支持泛型类型,所以你可能必须这样做JavaCast<T>();

因此,我会修改我的代码,使其看起来更像:

private SupportMapFragment mapFragment;
private GoogleMap map;

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    SetContentView (Resource.Layout.Main);

    SetUpMapIfNeeded();
}

public override void OnResume()
{
    base.OnResume();
    SetUpMapIfNeeded();
}

private void SetUpMapIfNeeded()
{
    if(null != map) return;

    mapFragment = SupportFragmentManager.FindFragmentById(Resource.Id.map).JavaCast<SupportMapFragment>();
    if (mapFragment != null)
    {
        map = mapFragment.Map;

        if (map == null)
        {
            // throw error here, should never happen though...
            return;
        }

        // set up map here, i.e.:
        map.UiSettings.CompassEnabled = true;
        map.MapType = GoogleMap.MapTypeHybrid;
        map.MyLocationChange += MapOnMyLocationChange;

        CircleOptions circle = new CircleOptions ();
        circle.InvokeCenter (new LatLng(18.5203,73.8567));
        circle.InvokeRadius (1000);
        map.AddCircle (circle);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SupportMapFragment 地图为空 的相关文章

  • Android平台源码中哪里可以找到版本信息

    Android 平台源文件中的版本信息在哪里找到 我尝试查找 设置 gt gt 中列出的有关手机的一些信息 显示的一些信息包括固件版本 模块编号 基带版本 内核版本 你可以给它办理登机手续platform build core versio
  • IntentService、Service 或 AsyncTask

    实现这一点的最佳方法是什么 我有一个 Android 应用程序 它将使用我的 python 服务器来允许两部手机之间进行轮流通信 回合意味着他们在一轮开始之前不能互相交谈 一旦他们发送一条消息 他们就不能发送另一条消息 直到对方做出回应 然
  • Android 从键盘读取

    我的登录屏幕根本没有文本字段 当用户使用 RFID 扫描仪扫描他的 id 令牌时 我会得到一个 8 个字符长的字符串 其原理与使用键盘相同 只是更快 我希望我的登录活动在用户扫描其令牌时而不是之前执行 有一个聪明的方法来实现这个吗 我不能有
  • Mesibo 通话 UI 未更新

    我正在尝试更改 Mesibo Call UI 的配置 但它并没有改变 我尝试如下 MesiboCallConfig mesiboCallConfig new MesiboCallConfig mesiboCallConfig backgro
  • Android-全屏视频视图

    我正在尝试使此 VideoView 以全屏模式显示 public class ViewVideo extends Activity private String filename private static final int INSER
  • Android/Java 创建辅助类来创建图表

    Goal 创建用于图形生成的辅助类 背景 我有 3 个片段 每个片段收集一些传感器数据 加速度计 陀螺仪 旋转 并使用 GraphView 绘制图表 以下是其中一个片段的代码 该代码当前工作正常 public class Gyroscope
  • 无法找到/下载 AppCompat-v7:23.1.1

    怎么了 我遇到了很多 找不到 appcompat v7 23 1 1 的问题 许多解决方案都不起作用 经过几个小时的思考和寻找答案 我遇到了一个奇怪的问题 I have gotAndroid 支持库 23 1 1 已安装 所有功能 exce
  • 如何找到特定路线上两点之间的距离?

    我正在为我的大学开发一个 Android 应用程序 可以帮助学生跟踪大学巴士的当前位置 并为他们提供巴士到达他们的预计时间 截至目前 我获取了公交车的当前位置 通过公交车上的设备 和学生的位置 我陷入了必须找到两个 GPS 坐标之间的距离的
  • 如何将 Google Now 搜索栏添加到我的应用程序中?

    谷歌刚刚将其搜索栏从 Google Now 引入到了 Play 商店应用程序中 如下面的 gif 所示 如何将这个操作栏搜索栏实现到我自己的应用程序中 我想要 style 汉堡动画 从工具栏按钮访问 麦克风按钮 对棒棒糖设备的连锁反应 我已
  • 将 firebase auth 与 google app engine 云端点集成

    有人可以指定 使用一些示例代码 如何验证谷歌云端点中的 firebase 令牌吗 最近提出的问题根本没有澄清 如何将 Firebase 身份验证与 Google 应用引擎端点集成 https stackoverflow com questi
  • 如何在 google.maps.event.addListener 中使用它

    以下示例有效 但是当我尝试传递参数并使用this在该功能不起作用 Working google maps event addListener markers i click showInfoWindow function showInfoW
  • Android 中带有透明背景的 ImageButton [重复]

    这个问题在这里已经有答案了 我已经按照这篇文章在android中制作ImageButton 安卓图像按钮 https stackoverflow com questions 2283444 android image button 图像出现
  • 从 Handler.obtainMessage() 获取什么参数

    我正在使用线程来执行一些 BT 任务 我正在尝试向 UI 线程发送消息 以便我可以基于我的 BT 线程执行 UI 工作 为此 我使用处理程序 但我不知道如何检索发送到处理程序的数据 要发送数据 我使用 handler obtainMessa
  • 如何在 Android 应用程序中使用多个 Graph API 获取 Facebook Notes 项目的评论?

    我想使用 Graph API 显示 Facebook 页面的注释项目以及这些评论和点赞 为此 我使用 Facebook SDK 中的 asyncFacebookRunner 步骤是这样的 调用 asyncFacebookRunner req
  • 与 Dagger 一起使用时,Espresso 生成 FileNotFoundException

    我一直在研究旧版 Android 应用程序 尝试为其添加测试和适当的架构 该应用程序有一个主要LaunchActivity它在启动时运行一系列检查 最初 该活动使用 Dagger 来 注入依赖项 活动将使用它来运行检查 但效果相当糟糕 我转
  • InAppMessage 一旦显示就会自动消失

    您好 我最近将 InAppMessaging 添加到我的项目中 这似乎很容易集成 但对我来说并没有按预期工作 首先 我将其添加到 build gradle 中 implementation com google firebase fireb
  • 使用 AndroidX ExifInterface 从图像中检索 GPS EXIF 数据?

    我的目标是 Android 13 并使用新的照片选择器 https developer android com training data storage shared photopicker检索图像 例如 val photoPicker
  • 如何在 Android 中保存 Edittext 中的文本而不丢失文本的粗体、斜体等功能

    我想做的就是从 Edittext 中获取文本 该文本具有粗体和斜体等功能 并将其保存在文本文件中 但是当我读回并显示它时 这些功能丢失了 它们不显示 如何通过将文本保存在文本文件或任何文件中来保持丰富的功能 您可以使用Html toHtml
  • 在上下文操作模式下选择时,ListView 项目不会在视觉上“突出显示”

    我关注了 Android 官方网站创建上下文操作菜单的教程 http developer android com guide topics ui menus html CAB 使用下面的代码 当我长按我的 ListView 项目之一时 它确
  • 在DialogFragment中,onCreate应该做什么?

    我目前正在摆弄 DialogFragment 以学习使用它 我假设相比onCreateView onCreate 可以这样做 public void onCreate Bundle savedInstanceState super onCr

随机推荐