java - 在布局中找不到 MapFragment,返回 null

标签 java android google-maps

您好,我在应用程序中有以下布局,它有一个 MapFragment,它加载 map 但是当我尝试获取它的引用时它在代码中返回 null。

主布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

这是 app_bar_main 布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activities.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

和具有 mapfragement 的 content_main 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activities.MainActivity"
tools:showIn="@layout/app_bar_main">

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

</RelativeLayout>

我用来获取 mapfragement 引用的代码位于 Activity 的 onCreate 下

 FragmentManager manager = getSupportFragmentManager();
    SupportMapFragment mapFragment = (SupportMapFragment) manager
            .findFragmentById(R.id.mapFragment);
    mapFragment.getMapAsync(this);

不知道为什么会崩溃

最佳答案

改变

android:name="com.google.android.gms.maps.MapFragment"


android:name="com.google.android.gms.maps.SupportMapFragment"

试试这个 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activities.MainActivity"
tools:showIn="@layout/app_bar_main">

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

</RelativeLayout>

您在 xml 中添加 MapFragment 并在 java 中获取 SupportMapFragment,这是代码中的异常。

关于java - 在布局中找不到 MapFragment,返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38042993/

相关文章:

android - 我们什么时候应该为对象选择 Lateinit ?将绑定(bind)设置为非可选和延迟初始化是一种不好的做法吗?

java - HttpURLConnection getResponseCode() 不起作用

android - 是否可以通过Google Places API获取 "Popular times"信息

java - 谷歌地图在 Android 中的选项卡 fragment 上显示灰色

android - 地理编码调用期间返回代码 610 - Android map

java - 根据枚举列表验证字符串列表

java - spark 2.0 - java.io.IOException : Cannot run program "jupyter": error=2, 没有这样的文件或目录

java - 将“设置” Activity 连接到“Activity 2”后,我的应用程序崩溃了

java - 将面板布局设置为 null 时出现 NullPointerException?

java - 无法转换 .toURI() 的 URL 示例?