我试图在
Android应用程序中显示地图.我正在使用地图V2.但抛出错误“二进制XML文件行#9:错误膨胀类片段”.该应用程序在发布时崩溃.
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>
解决方法
以下必须是清单文件的内部应用程序标记
参考
<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.
<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的地图.