首页 > 解决方案 > 带有 AndroidStudio 的 GoogleMaps 空白页

问题描述

我是 AndroidStudio 的初学者,所以我尝试构建一个基本的应用程序。我添加了一个 GoogleMap 活动,它运行良好。然后我将 Firebase 添加到应用程序中,我发现 GoogleMapActivity 不再工作了。它只是向我显示了一个空白页面,左下方带有“google”徽标。我试图在虚拟设备和真实设备上运行该程序。没有成功。

我试图更改密钥,我也清理/重建了应用程序。事实是.... logcat 没有显示任何消息,甚至没有警告。

我真的不明白为什么 GoogleMapActivity 以前可以工作,并且不再工作了。

您必须知道,当我添加 Firebase 时,我更改了很多依赖项。也许这是 SDK 版本控制的问题?我不知道...

这是我在 AndroidManifest.xml 中的权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>


    <permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.barcodelibrary.permission.MAPS_RECEIVE"/>

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_maps_key" />
<meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

当然,我在google_maps_api.xml文件中有一个有效的 Google API 密钥,看起来像这样。

<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">[i have a real key but i don't know if i can show it here ?]</string>

这是我的build.gradle (module:app)文件:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.kandel.myandroidapp"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:support-annotations:28.0.0-alpha3'
    implementation 'com.google.firebase:firebase-auth:16.0.2'
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-database:16.0.1'

    implementation 'com.google.android.gms:play-services:12.0.1'
    implementation 'com.google.android.gms:play-services-maps:15.0.1'
    implementation 'com.google.android.gms:play-services-auth:15.0.1'
    implementation 'com.google.android.gms:play-services-identity:15.0.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

EDIT1:添加我的 GoogleMapActivity 代码

如您所见,它是 AndroidStudio 提供的基本活动。我想念什么吗?

package com.kandel.myandroidapp;

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);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @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));
    }
}

编辑 2:添加我的 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" />

标签: androidgoogle-mapsandroid-studiogoogle-api

解决方案


您应该在生成密钥时添加您的 SHA-1 指纹,您将从您的 android studio 或命令行获取它。您可以通过以下链接生成它,并在生成地图的密钥时使用它。

密钥库证书的 SHA-1 指纹

使用 android studio 生成它的步骤:

  1. 进入android studio右侧的gradle文件。
  2. 扩展您的项目
  3. 转到应用程序文件夹,然后单击 android
  4. 选择签名报告以生成它。5)你可以在底部的Run空间找到它,关键字为SHA1

推荐阅读