0

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: クラス フラグメントの膨張エラー

4

3 に答える 3

1

エミュレーターが Google API をサポートしていることを確認してください。

そうでない場合は、Google API を含む新しいエミュレーターを起動し、この新しいエミュレーターでコードを実行してください。

于 2013-01-22T09:13:45.003 に答える
0

Google Play Service Lib が最初にプロジェクトに追加されているかどうかを確認する Pratik に同意します。

すでにプロジェクトにある場合は、おそらく minSDKversion が 8 しかなく、MapFragment が機能するための最低 11 ではないためです。そのため、MapFragment を使用する代わりに、代わりに SupportMapFragment を使用する必要があります。

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" />
于 2013-01-22T10:13:55.960 に答える
0

を実行するにはGoogle Map v2が必要ですがGoogle Play Services、知る限りエミュレータには が含まれていないGoogle Play Servicesため、エミュレータで Google マップを実行することはできません。

アプリで Google Maps Android API v2 を使用するには、まず Google Play サービス SDK をインストールする必要があります。

詳細については、リンクをチェックしてください

于 2013-01-22T09:08:03.877 に答える