如何在ConstraintLayout中显示GoogleMap? [英] How to show GoogleMap inside ConstraintLayout?

查看:132
本文介绍了如何在ConstraintLayout中显示GoogleMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新项目"MapsActivity".我从Google获得了API密钥,并将该API密钥放置在YOUR_KEY_HERE区域内的google_maps_API.xml(debug)中.我在AndroidManifest中看到了它.然后我去构建/运行该应用程序,它崩溃了.

I created a new project "MapsActivity". I got my API key from Google, placed the API key in google_maps_API.xml(debug) inside the YOUR_KEY_HERE area. I see in AndroidManifest it's there. Then I go to build/run the app and it crashes.

我什至还没有编写任何代码.

I didn't even code anything yet.

根据Android Monitor,这是崩溃报告:

According to Android Monitor here's the crash report:

 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,然后将我带到以下内容:

So I click on the MapsActivity.java:24 and it takes me to the following:

[![在此处输入图片描述] [1]] [1]

[![enter image description here][1]][1]

请记住,我还没有触摸任何代码,只是创建了这个新项目,只在我应该使用的位置添加了API密钥,然后单击了build/run,然后崩溃了.这是所有Google创建的崩溃代码.

Remember, I didn't touch any code yet, just created this new project, only added the API key where I was supposed to and clicked build/run and then it crashes. This is all Google created code that crashes.

我查看了StackOverflow,并尝试按照人们的说法从getSupportFragmentManager更改为getChildFragmentManager,这是行不通的.

I looked on StackOverflow and tried changing from getSupportFragmentManager to getChildFragmentManager as people say and it doesn't work.

activity_maps.xml文件:

activity_maps.xml file:

<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" />

怎么了?我必须更改什么?

What's wrong? What do I have to change?

标签允许布局文件在运行时动态包含不同的布局.在布局编辑时,尚不知道要使用的特定布局.您可以在编辑布局时选择要预览的布局.

A tag allows a layout file to dynamically include different layouts at runtime. At layout editing time the specific layout be used is no known. You can choose which layout you would like previewed while editing the layout.

  • <分段 com.google.android.gms.maps.SupportMapFragment ...> (选择布局)
  • < fragment com.google.android.gms.maps.SupportMapFragment...> (Pick Layout)

推荐答案

您需要将Fragment放在ConstraintLayout内:

You need to put the Fragment inside the 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>

这篇关于如何在ConstraintLayout中显示GoogleMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆