1

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

这是我的清单文件:

<?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.
4

5 回答 5

4

你应该使用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"/>

您的活动必须扩展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 启用了地图

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

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

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

应用程序显示网格屏幕而不是谷歌地图

当我运行我的应用程序时,在模拟器上仅显示放大控件,其余屏幕为白色

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

于 2013-06-22T17:17:20.207 回答
0

您应该使用 SupportMapFragment,因为您的 API 级别是 2.3.5

将 XML 中的片段替换为:

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

并在 Java 文件中导入这个类,而不是 MapFragment。

此外,作为一般建议,如果您想支持预蜂窝设备并使用片段等,您应该使用支持库。

于 2013-06-22T15:51:20.867 回答
0

我遇到了同样的错误并尝试了 6 个教程,并阅读了谷歌的所有文档。最后我让它运行并显示地图。这就是它对我的工作方式。

1.从 Eclipse 和 PC 中删除库 google-play-services 和 android support-v4。2.从 de sdk-manager 再次下载。3.按照说明 [此处] ( https://developers.google.com/maps/documentation/android/intro#sample_code ) 转到示例代码并按照说明进行操作。特别检查这个 Existing Android Code Into Workspace,不要将库作为项目导入,它们将不起作用。4.将您的所有 MapFragment 更改为 SupportMapFragment,不要忘记将您的 Activity 扩展到 FragmentActivity(那是我的失败)。5.禁用(如果已启用)项目中的自动构建选项。6.清理项目。5.启用自动构建选项。

它应该工作。

于 2013-09-20T08:12:45.583 回答
0
You have to use FragmentActivity

public class PlacesMapActivity extends android.support.v4.app.FragmentActivity
    implements OnClickListener, LocationListener {

ImageButton btn_home;
private GoogleMap mMap;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mMap = fragment.getMap();
    mMap.setMyLocationEnabled(true);

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

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.title("First Location");
    markerOptions.snippet("This Is Test Location");

    LatLng latlng = new LatLng(23.0333, 72.6167);

    markerOptions.position(latlng);
    // markerOptions.title("Ahmedabad Cordinat Found here");

    // Marker m = mMap.addMarker(markerOptions);

    mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) {

            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {

            View myContentView = getLayoutInflater().inflate(
                    R.layout.custommarker, null);
            TextView tvTitle = ((TextView) myContentView
                    .findViewById(R.id.title));
            tvTitle.setText(marker.getTitle());
            TextView tvSnippet = ((TextView) myContentView
                    .findViewById(R.id.snippet));
            tvSnippet.setText(marker.getSnippet());
            return myContentView;

        }
    });

    mMap.addMarker(new MarkerOptions()
            .position(latlng)
            .title("This is Sabarmati Ashram")
            .snippet("Ahmedabad")
              .icon(BitmapDescriptorFactory                     .defaultMarker(BitmapDescriptorFactory.HUE_RED)));

    mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getBaseContext(),
                    DetailsOfPlacesActivity.class);
            startActivity(intent);
        }
    });

    btn_home = (ImageButton) findViewById(R.id.activity_map_ibtn_home);
    btn_home.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });
}
于 2013-07-12T12:10:24.160 回答
0

您使用的是哪种 API 密钥?编程时应始终使用开发密钥,并且您的应用需要在 Google API 控制台中注册。

而且您必须在Preferences/Android中添加 google-play-services-library ,而不是在Preferences/Java Build Path中。

于 2013-06-22T16:49:59.810 回答