【问题标题】:google maps white screen谷歌地图白屏
【发布时间】:2018-01-13 16:52:05
【问题描述】:

我正在尝试在我的应用程序中实现谷歌地图,我在用户登录后实现了注册和登录活动,但我想显示一个地图,它显示为一个空白屏幕。 我已生成 API 密钥并将其添加到我的 google_maps_api.xml

当我创建一个新项目并向其添加地图活动时,它可以正常工作,我认为他们的问题出在我的项目中。

我已经在模拟器和三星设备上测试过了。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.x.x.x">

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

<!-- 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"/>
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key"/>

    <activity
            android:name=".Activitys.MainActivity"
            android:screenOrientation="portrait">
    </activity>
    <activity
            android:name=".Activitys.Register"
            android:screenOrientation="portrait">
    </activity>
    <activity
            android:name=".Activitys.Register2"
            android:screenOrientation="portrait">
        android:noHistory="true"
    </activity>
    <activity
            android:name=".Activitys.Login"
            android:screenOrientation="portrait">
    </activity>
    <activity android:name=".Activitys.Family_setup">
    </activity>
    <activity android:name=".Activitys.Join_family">
    </activity>
    <activity android:name=".Activitys.Create_family">
    </activity>
    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->

    <activity
            android:name=".Activitys.MapsActivity"
            android:label="@string/title_activity_maps">

        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

出于测试目的,我已将地图活动设置为首先开始。

当我运行应用程序时,我得到一个白屏,左下角有谷歌的标志。

image

【问题讨论】:

  • Google 地图需要 API 密钥才能工作,developers.google.com/maps/documentation/android-api/signup
  • 我已经添加了API密钥,但它仍然只显示白屏。
  • 检查您的日志是否有错误
  • 错误是授权失败,虽然我确定我使用了正确的 API 密钥。
  • 同时发布错误消息并仔细检查 API 密钥是否有正确的限制。

标签: android google-maps screen


【解决方案1】:

感谢您的帮助。

我已将活动名称从android:name=".Activitys.MapsActivity" 更改为android:name="com.project.me.appname.Activitys.MapsActivity",这为我解决了问题。

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,只是想说谢谢你分享你的解决方案。我还想补充一点:

    1) 你也可以在谷歌的开发者控制台中编辑包名,所以你不需要在你的应用程序中重命名包和依赖项。 您也可以提供您的根包名称,而不必将密钥限制为一个活动

    2) 通过 USB 在我的手机上调试效果很好,但模拟器仍然无法渲染地图

    3) 您的应用程序的发布版本需要不同的 API 密钥,因为 API 密钥绑定到您的 apk 的 SHA1,这与调试模式和发布模式不同。您的 res 文件夹中实际上有 2 个单独的文件/目录。

    【讨论】:

      【解决方案3】:

      在遇到此错误 2 天后,我找到了一个非常好的解决方案。

      实际上,我通过在模拟器上安装发布签名的 apk 并检查显示的 Logcat 来调试应用程序:

      E/Google Maps Android API﹕ Authorization failure.  Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
      E/Google Maps Android API﹕ In the Google Developer Console (https://console.developers.google.com)
      Ensure that the "Google Maps Android API v2" is enabled.
      Ensure that the following Android Key exists:
      API Key: YOUR_KEY_HEREAndroid Application (<cert_fingerprint>;<package_name>): 
      <SHA1 Removed for this> ;com.xaif.mapstest
      

      这是因为您的应用的发布版本具有不同的 SHA-1 指纹。 因此,为已发布版本生成一个新的 SHA-1 指纹,并将其添加到 Credentials 中的 Restriction 部分,并具有相同的包名。这需要 5-10 分钟才能添加。现在,您的应用程序运行完美。

      【讨论】:

        猜你喜欢
        • 2018-07-17
        • 1970-01-01
        • 1970-01-01
        • 2013-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-27
        • 1970-01-01
        相关资源
        最近更新 更多