谷歌地图不会在模拟器上显示

时间:2013-01-22 08:59:04

标签: android

XML

<RelativeLayout 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"
tools:context=".MainActivity" >

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

  </RelativeLayout>

活动

 package com.example.googlemap;
 import android.annotation.SuppressLint; 
 import android.os.Bundle;
 import android.support.v4.app.FragmentActivity;

 import com.google.android.gms.maps.CameraUpdateFactory;
 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.MapFragment;
 import com.google.android.gms.maps.model.BitmapDescriptorFactory;
 import com.google.android.gms.maps.model.LatLng;
 import com.google.android.gms.maps.model.Marker;
 import com.google.android.gms.maps.model.MarkerOptions;


 public class MainActivity extends FragmentActivity {

  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;
//@SuppressLint("NewApi")

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

    // SupportMapFragment fragment = new SupportMapFragment();
      //  getSupportFragmentManager().beginTransaction()
         //       .add(android.R.id.content, fragment).commit();
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))   .getMap();
        Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
            .title("Hamburg"));
        Marker kiel = map.addMarker(new MarkerOptions()
            .position(KIEL)
            .title("Kiel")
            .snippet("Kiel is cool")
            .icon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher)));

        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

}

}

清单

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

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


<permission
    android:name="com.example.googlemap.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-permission android:name="com.example.googlemap.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<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" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.googlemap.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="AIzaSyCcdbqI_CdaghJQ7VTeXIE1djmnlfZq_Qs" />
  </application>

</manifest>

但代码不起作用, 像这样的错误

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.googlemap / com.example.googlemap.MainActivity}:android.view.InflateException:二进制XML文件行#7:错误扩充类片段

3 个答案:

答案 0 :(得分:1)

确保您的模拟器支持Google API。

如果不是启动包含Google API的新模拟器,并使用这个新模拟器运行您的代码。

答案 1 :(得分:0)

Google Map v2要求Google Play Services运行,AFAIK模拟器不包含Google Play Services,因此您无法在模拟器中运行Google地图。

要在您的应用中使用Google Maps Android API v2,您首先需要安装Google Play服务SDK。

有关详细信息,请查看Link

答案 2 :(得分:0)

我同意Pratik检查您的Google Play Service Lib是否已首先添加到您的项目中。

如果它已经存在于你的项目中,可能是因为你的minSDKversion只有8而且MapFragment的最小值不是11,因此你需要使用SupportMapFragment来代替使用MapFragment。

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

需要更改为SupportMapFragment 所以:

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />
相关问题