简体   繁体   English

来自Google Maps API的Android Studio预览地图不会显示地图

[英]Android Studio Preview map from google maps API won't display a map

I've created a New project through the wizard, and chosen "Google Maps Activity". 我已经通过向导创建了一个New项目,然后选择了“ Google Maps Activity”。 Inserted my API key, but there is no map preview, just a gray screen that says . 插入了我的API密钥,但没有地图预览,只有显示的灰色屏幕。 There is also a warning called Unknown fragments that says: 还有一个名为“未知片段”的警告,内容为:

"A <fragment> tag allows a layout file to dynamically include different layouts at runtime. At layout editing time the specific layout to be used is not known. You can choose which layout you would like previewed while editing the layout.
- <fragment com.google.android.gms.maps.SupportMapFragment ...> (Pick Layout...)
 Do not warn about <fragment> tags in this session"

I've been searching in the web for hours and hours, but could not find a way to fix it. 我已经在网上搜索了好几个小时,但是找不到解决它的方法。 If someone can, please let me know, I would really appreciate it. 如果有人可以,请告诉我,我将非常感谢。

This is the code from activity_maps.xml 这是来自activity_maps.xml的代码

<?xml version="1.0" encoding="utf-8"?> <fragment 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=".MapsActivity"
    tools:layout="@layout/activity_maps" />

And this is the code from MapsActivty.java 这是MapsActivty.java中的代码

>     package com.example.android.myapplication;
>     
>     import android.support.v4.app.FragmentActivity;
>     import android.os.Bundle;
>     
>     import com.google.android.gms.maps.CameraUpdateFactory;
>     import com.google.android.gms.maps.GoogleMap;
>     import com.google.android.gms.maps.OnMapReadyCallback;
>     import com.google.android.gms.maps.SupportMapFragment;
>     import com.google.android.gms.maps.model.LatLng;
>     import com.google.android.gms.maps.model.MarkerOptions;
>     
>     public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
>     
>         private GoogleMap mMap;
>     
>         @Override
>         protected void onCreate(Bundle savedInstanceState) {
>             super.onCreate(savedInstanceState);
>             setContentView(R.layout.activity_maps);
>             // Obtain the SupportMapFragment and get notified when the map is ready to be used.
>             SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
>                     .findFragmentById(R.id.map);
>             mapFragment.getMapAsync(this);
>         }
>     
>         @Override
>         public void onMapReady(GoogleMap googleMap) {
>             mMap = googleMap;
>     
>             // Add a marker in Sydney and move the camera
>             LatLng sydney = new LatLng(-34, 151);
>             mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
>             mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
>         }
>     }

Fragments are not attached to an activity until runtime. 片段直到运行时才附加到活动。 Android Studio does not have knowledge of what fragment you're trying to preview. Android Studio不了解您要预览的片段。 The warning you got says it all. 您得到的警告说明了一切。 This is not an issue, but rather a limitation of the Android Studio designer. 这不是问题,而是Android Studio设计人员的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM