![](/img/trans.png)
[英]How to fix the android.view.InflateException: Error inflating class fragment when using google maps v2 fragment
[英]Android Google Map API v2 - Error Inflating Class Fragment android.view.InflateException
我正在遵循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>
请帮助我解决此错误。
你应该改变
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" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.