java - 我的 Google map fragment 显示灰色。我无法显示实际 map

标签 java android android-studio google-maps google-maps-api-3

我正在尝试将 Google map fragment 添加到选项卡式布局中,但 map 仅显示为灰色 fragment ,且 Google Logo 位于左下角。我的 API key 有效,并且我已通过 Google Cloud Platform 启用了 API 和 SDK。

MapFragment.java

public class MapFragment extends Fragment implements OnMapReadyCallback {

    private GoogleMap mMap;
    private View mView;
    MapView mapView;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mapView = getView().findViewById(R.id.mapView);

        mapView.onCreate(savedInstanceState);
        mapView.onResume();

        mapView.getMapAsync(this);
    }

    public MapFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_map, container, false);
        return mView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mapView = (MapView) mView.findViewById(R.id.map);
        if(mapView != null) {
            mapView.onCreate(null);
            mapView.onResume();
            mapView.getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap map) {

        mMap = map;
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    }

fragment_map.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapFragment">

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:apiKey="AIzaSyC-MdT7T9uXkL0GmpbsMeWJsBwhorqAc3c"
        android:clickable="true"
        android:focusable="true"
        android:visibility="visible"
        tools:layout_conversion_absoluteHeight="603dp"
        tools:layout_conversion_absoluteWidth="411dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />
</FrameLayout>

Screenshot of my app.

解决方案 很多时候错误与凭据有关。尽管在 Google Cloud 中启用了所有必要的 API,但我重新制作了凭据,但错误仍然存​​在。错误在于,尽管更改了 API key ,原始凭据仍保留在模拟器上的应用程序中。在模拟器(或设备)上,转到设置>应用程序管理器>选择您的应用程序>单击清除数据

另一种解决方案是完全卸载应用程序并在重新制作凭据后重新加载。

最佳答案

这个问题已经被问过很多次了。十分之九是无效 key 。删除您的 key 并生成一个新 key 。

关于java - 我的 Google map fragment 显示灰色。我无法显示实际 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61706370/

相关文章:

android - 如何在我的 Android 项目中启动 Quick Office 应用程序?

android - Android Studio 3.6.3错误:脱机模式没有com.android.tools.build:gradle:3.6.3的缓存版本。 Gradle 版本:5.6.4

python - 在 Android Studio 中运行 Python 脚本

java - 如何在 iText pdf 中旋转 JFreeChart

java - 如何为 SOCKS 代理设置 nonProxyHosts?

java - 如何在 Tab 布局中设置 Tab 宽度?

android - 如何从多台计算机进行 Android 开发?

android - 在 Android Studio 中降级 Android 插件

java RGB颜色空间到YCrCb颜色空间转换

java - Android Camera - 只存储图片的一部分