我已经通过向导创建了一个新项目,并选择了“ Google Maps Activity”。插入了我的API密钥,但没有地图预览,只有显示的灰色屏幕。还有一个名为“未知片段”的警告,内容为:
"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"
我已经在网上搜索了好几个小时,但是找不到解决方法。如果有人可以,请告诉我,我将非常感谢。
这是来自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" />
这是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));
> }
> }
答案 0 :(得分:1)
片段直到运行时才附加到活动。 Android Studio不了解您要预览的片段。您得到的警告说明了一切。这不是问题,而是Android Studio设计者的限制。