android:二进制 XML 文件第 9 行:错误膨胀类片段 [英] android: Binary XML file line #9: error inflating class fragment
问题描述
我试图在 android 应用程序中显示地图.我正在使用地图 V2.但是会抛出错误二进制 XML 文件第 9 行:错误膨胀类片段".应用在启动时崩溃.
I was trying to show the maps in android application. I am using map V2. But the error "Binary XML file line #9: error inflating class fragment" is thrown. The app is crashed on launch.
下面是我的代码.请帮我解决这个问题
Below is my code. Please help me in solving this stuff
MainActivity.Java
package com.example.googlemapsv2;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Android 清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.googlemapsv2.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>
</application>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBAvQK4RAin10dqyflr95tH45Ce_Eko3G0" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</manifest>
推荐答案
以下内容必须在manifest文件的application
标签内
The below must be inside application
tag of manifest file
参考
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBAvQK4RAin10dqyflr95tH45Ce_Eko3G0" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
因为你的 minsdk 是 12.你需要使用 MapFragment
并且你的类应该扩展 Activity
.
Since your minsdk is 12. You need to use MapFragment
and your class should extend Activity
.
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
然后
public class MainActivity extends Activity
确保您正确引用了库项目并在 google api 控制台中为 android 启用了地图.
Make sure you reference the library project properly and enabled maps for android in the google api console.
这篇关于android:二进制 XML 文件第 9 行:错误膨胀类片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!