如何在 ConstraintLayout 中显示 GoogleMap?
How to show GoogleMap inside ConstraintLayout?
我创建了一个新项目"MapsActivity"。我从 Google 得到了我的 API 密钥,将 API 密钥放在 YOUR_KEY_HERE 区域的 google_maps_API.xml(调试)中。我在 AndroidManifest 中看到了它。然后我转到 build/run 应用程序,它崩溃了。
我什至还没有编写任何代码。
根据 Android Monitor 的崩溃报告:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at com.example.mikedouglas.maps.MapsActivity.onCreate(MapsActivity.java:24)
所以我点击 MapsActivity.java:24,它会带我到以下内容:
[![在此处输入图片描述][1]][1]
记住,我还没有接触任何代码,只是创建了这个新项目,只在我应该添加的地方添加了 API 键并单击了 build/run 然后它崩溃了。这是所有 Google 创建的崩溃代码。
我查看了 Whosebug 并尝试按照人们所说的那样从 getSupportFragmentManager 更改为 getChildFragmentManager,但它不起作用。
activity_maps.xml 文件:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mikedouglas.maps.MapsActivity" />
怎么了?我需要更改什么?
编辑:
标签允许布局文件在运行时动态包含不同的布局。在布局编辑时,所使用的特定布局是未知的。您可以在编辑布局时选择要预览的布局。
- <片段
com.google.android.gms.maps.SupportMapFragment...>
(选择布局)
您需要将 Fragment 放入 ConstraintLayout 中:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mikedouglas.maps.MapsActivity">
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
我创建了一个新项目"MapsActivity"。我从 Google 得到了我的 API 密钥,将 API 密钥放在 YOUR_KEY_HERE 区域的 google_maps_API.xml(调试)中。我在 AndroidManifest 中看到了它。然后我转到 build/run 应用程序,它崩溃了。
我什至还没有编写任何代码。
根据 Android Monitor 的崩溃报告:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at com.example.mikedouglas.maps.MapsActivity.onCreate(MapsActivity.java:24)
所以我点击 MapsActivity.java:24,它会带我到以下内容:
[![在此处输入图片描述][1]][1]
记住,我还没有接触任何代码,只是创建了这个新项目,只在我应该添加的地方添加了 API 键并单击了 build/run 然后它崩溃了。这是所有 Google 创建的崩溃代码。
我查看了 Whosebug 并尝试按照人们所说的那样从 getSupportFragmentManager 更改为 getChildFragmentManager,但它不起作用。
activity_maps.xml 文件:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mikedouglas.maps.MapsActivity" />
怎么了?我需要更改什么?
编辑:
标签允许布局文件在运行时动态包含不同的布局。在布局编辑时,所使用的特定布局是未知的。您可以在编辑布局时选择要预览的布局。
- <片段 com.google.android.gms.maps.SupportMapFragment...> (选择布局)
您需要将 Fragment 放入 ConstraintLayout 中:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mikedouglas.maps.MapsActivity">
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>