Android谷歌地图API v2 - 错误膨胀类碎片android.view.InflateException

时间:2015-02-02 10:50:03

标签: java android xml google-maps android-fragments

我正在关注Google Maps Android API v2文档,以便在我的应用程序中使用地图。我收到错误“”我已尝试过所有内容,但仍然无法解决问题。

这是我的 MainActivity.java

package com.example.maps;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity {

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

这是 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="${relativePackage}.${activityClass}" >

  <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

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

</RelativeLayout>

这是我在logcat中得到的错误

02-02 16:02:46.110: E/AndroidRuntime(11094): FATAL EXCEPTION: main
02-02 16:02:46.110: E/AndroidRuntime(11094): Process: com.example.maps, PID: 11094
02-02 16:02:46.110: E/AndroidRuntime(11094): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maps/com.example.maps.MainActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class fragment

的Manifest.xml

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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="19" />
    <permission 
        android:name="com.example.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"
        ></permission>
    <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"/>
    <!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <uses-feature 
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/gyan_logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name="com.example.maps.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>
        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                    android:name="com.google.android.gms.maps.MapFragment"
                    android:id="@+id/map"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyBJ4S3tVGq4GWjgsuJ2zm0EAfUPN3yFKKA"/>
    </application>

</manifest>

请帮我解决此错误。

1 个答案:

答案 0 :(得分:1)

你应该改变

  android:name="com.google.android.gms.maps.MapFragment"

  android:name="com.google.android.gms.maps.SupportMapFragment"

因为您使用的是FragmentActivity

编辑:

您还需要添加manifest.xml

 <meta-data
   android:name="com.google.android.gms.version"
   android:value="@integer/google_play_services_version" />