android - Google Maps API v2 无法显示 map

标签 android google-maps

我是 Stackoverflow 的新手。我在新的 Google Maps API v2 上显示 map 时遇到问题。 我完成了此页面 https://developers.google.com/maps/documentation/android/start?hl=pl 中的所有步骤ans 一些教程,我无法显示 map 。当我运行我的应用程序时,在模拟器上仅显示放大控件,其余屏幕为白色,我无法在我的真实设备(HTC Wildfire S-android 2.3.5)上打开它。我也将 google_play_service_lib 添加到我的项目中。我在很多页面和很多教程中寻找我的问题的答案,但我没有找到它!请帮助我!

这是我的 list 文件:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.adrianspora.googlemaptest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyD6r1gse4Jx4sTrCECvLQQwgxLJikHm4Ow"/>
    </application>
    <permission
        android:name="com.adrianspora.googlemaptest.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
<uses-permission android:name="com.adrianspora.googlemaptest.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

</manifest>

有MainActivity的代码:

package com.adrianspora.googlemaptest;

import com.google.android.gms.maps.MapFragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是主要布局的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


<fragment 
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

</LinearLayout>

LogCat 中显示了一个日志列表:

06-22 14:51:44.897: D/AndroidRuntime(1092): Shutting down VM
06-22 14:51:44.927: W/dalvikvm(1092): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-22 14:51:44.996: E/AndroidRuntime(1092): FATAL EXCEPTION: main
06-22 14:51:44.996: E/AndroidRuntime(1092): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adrianspora.googlemaptest/com.adrianspora.googlemaptest.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.os.Looper.loop(Looper.java:137)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at java.lang.reflect.Method.invokeNative(Native Method)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at java.lang.reflect.Method.invoke(Method.java:511)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at dalvik.system.NativeStart.main(Native Method)
06-22 14:51:44.996: E/AndroidRuntime(1092): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.app.Activity.setContentView(Activity.java:1881)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at com.adrianspora.googlemaptest.MainActivity.onCreate(MainActivity.java:15)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.app.Activity.performCreate(Activity.java:5104)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-22 14:51:44.996: E/AndroidRuntime(1092):     ... 11 more
06-22 14:51:44.996: E/AndroidRuntime(1092): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
06-22 14:51:44.996: E/AndroidRuntime(1092):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
06-22 14:51:44.996: E/AndroidRuntime(1092):     ... 21 more
06-22 15:00:25.848: D/dalvikvm(1273): GC_CONCURRENT freed 256K, 15% free 2600K/3032K, paused 85ms+37ms, total 193ms
06-22 15:00:26.236: D/dalvikvm(1273): GC_CONCURRENT freed 272K, 14% free 2742K/3188K, paused 72ms+5ms, total 116ms
06-22 15:00:26.267: E/Google Maps Android API(1273): Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above
06-22 15:00:26.487: D/dalvikvm(1273): GC_FOR_ALLOC freed 152K, 11% free 2895K/3224K, paused 40ms, total 41ms
06-22 15:00:26.747: D/dalvikvm(1273): GC_CONCURRENT freed 85K, 8% free 3271K/3540K, paused 22ms+87ms, total 155ms
06-22 15:00:27.057: D/libEGL(1273): Emulator without GPU support detected. Fallback to software renderer.
06-22 15:00:27.087: I/Choreographer(1273): Skipped 56 frames!  The application may be doing too much work on its main thread.
06-22 15:00:27.127: D/libEGL(1273): loaded /system/lib/egl/libGLES_android.so
06-22 15:00:27.188: D/gralloc_goldfish(1273): Emulator without GPU emulation detected.
06-22 15:00:28.117: D/dalvikvm(1273): GC_CONCURRENT freed 266K, 12% free 3470K/3912K, paused 36ms+7ms, total 128ms
06-22 15:00:28.117: D/dalvikvm(1273): WAIT_FOR_CONCURRENT_GC blocked 79ms
06-22 15:00:28.216: D/dalvikvm(1273): GC_FOR_ALLOC freed 435K, 18% free 3340K/4044K, paused 45ms, total 51ms
06-22 15:00:28.348: D/dalvikvm(1273): GC_CONCURRENT freed 326K, 15% free 3470K/4044K, paused 15ms+8ms, total 69ms
06-22 15:00:28.348: D/dalvikvm(1273): WAIT_FOR_CONCURRENT_GC blocked 45ms
06-22 15:00:28.557: D/dalvikvm(1273): GC_CONCURRENT freed 551K, 19% free 3399K/4152K, paused 21ms+26ms, total 113ms
06-22 15:00:36.126: E/Google Maps Android API(1273): Authorization failure.  Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
06-22 15:00:36.136: E/Google Maps Android API(1273): Ensure that the following correspond to what is in the API Console: Package Name: com.adrianspora.googlemaptest, API Key: AIzaSyD6r1gse4Jx4sTrCECvLQQwgxLJikHm4Ow, Certificate Fingerprint: 1D915CBBCDEBB8CECC48371ED32EE0D5CA5DB443
06-22 15:14:09.636: E/Trace(1502): error opening trace file: No such file or directory (2)
06-22 15:14:11.196: D/dalvikvm(1502): GC_CONCURRENT freed 230K, 14% free 2599K/3004K, paused 124ms+91ms, total 328ms
06-22 15:14:11.386: E/Google Maps Android API(1502): Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above
06-22 15:14:11.626: D/dalvikvm(1502): GC_CONCURRENT freed 264K, 14% free 2789K/3228K, paused 73ms+86ms, total 226ms
06-22 15:14:11.796: D/dalvikvm(1502): GC_FOR_ALLOC freed 119K, 12% free 2913K/3280K, paused 37ms, total 38ms
06-22 15:14:12.106: D/dalvikvm(1502): GC_CONCURRENT freed 64K, 8% free 3288K/3556K, paused 71ms+73ms, total 208ms
06-22 15:14:12.477: D/libEGL(1502): Emulator without GPU support detected. Fallback to software renderer.
06-22 15:14:12.496: D/libEGL(1502): loaded /system/lib/egl/libGLES_android.so
06-22 15:14:12.536: I/Choreographer(1502): Skipped 69 frames!  The application may be doing too much work on its main thread.
06-22 15:14:12.656: D/gralloc_goldfish(1502): Emulator without GPU emulation detected.
06-22 15:14:13.307: D/dalvikvm(1502): GC_CONCURRENT freed 332K, 14% free 3358K/3868K, paused 143ms+137ms, total 477ms
06-22 15:14:13.307: D/dalvikvm(1502): WAIT_FOR_CONCURRENT_GC blocked 322ms
06-22 15:14:13.766: D/dalvikvm(1502): GC_CONCURRENT freed 365K, 15% free 3471K/4040K, paused 6ms+7ms, total 296ms
06-22 15:14:13.766: D/dalvikvm(1502): WAIT_FOR_CONCURRENT_GC blocked 97ms
06-22 15:14:13.866: D/dalvikvm(1502): GC_FOR_ALLOC freed 434K, 18% free 3342K/4040K, paused 39ms, total 46ms
06-22 15:14:14.056: D/dalvikvm(1502): GC_CONCURRENT freed 433K, 16% free 3400K/4040K, paused 12ms+27ms, total 109ms
06-22 15:14:18.076: E/Google Maps Android API(1502): Authorization failure.  Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
06-22 15:14:18.076: E/Google Maps Android API(1502): Ensure that the following correspond to what is in the API Console: Package Name: com.adrianspora.googlemaptest, API Key: AIzaSyD6r1gse4Jx4sTrCECvLQQwgxLJikHm4Ow, Certificate Fingerprint: 1D915CBBCDEBB8CECC48371ED32EE0D5CA5DB443
06-22 15:14:23.066: I/Choreographer(1502): Skipped 96 frames!  The application may be doing too much work on its main thread.

最佳答案

您应该使用 Support Fragment,因为您的 min sdk 是 8。

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/SupportMapFragment

<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"  
android:layout_width="match_parent"
android:layout_height="match_parent"/>

您的 Activity 必须扩展 FragmentActivity 并使用下面的代码初始化 GoogleMap 对象。

SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap(); 

确保你已经添加了支持库

还要确保您导入了以下内容

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;    

检查您是否在服务选项卡下的 google api 控制台中为 android 启用了 map

检查您是否遵循了文档中提到的所有步骤

https://developers.google.com/maps/documentation/android/start

你也可以查看我回答过的类似问题。

App shows grid screen instead of google map

When i run my application, on emulator displays only magnification controls and rest screen is white

您应该在真实设备上对其进行测试。

关于android - Google Maps API v2 无法显示 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252461/

相关文章:

android - Xamarin Android Google Maps App map 是空白的,没有报错

java - Android 应用程序的后端

Android - 选项卡式 fragment

java - JNI 参数是局部或全局引用

java - 删除 Google map 上之前的方向路线

javascript - 需要同时在MAP上按多边形绘制美国各县

android - 如何在单击按钮时从编辑文本更改焦点?

java - Android:使用 KML 文件和地理位置检测国家/地区

javascript - 为什么我的 Google Maps V3 标记位于屏幕中心,而不是定位在它们的地理位置上?

javascript - 信息窗口关闭和 var(google maps api v3)