【发布时间】:2014-01-13 04:06:11
【问题描述】:
我试图在 android 应用程序中显示地图。我正在使用地图 V2。但是会抛出错误“Binary XML file line #9: error inflating class fragment”。应用在启动时崩溃。
下面是我的代码。请帮我解决这个问题
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>
【问题讨论】:
-
将此
android:minSdkVersion="12"更改为11。 -
@Raghunandan:这对我没有任何帮助。我正在使用模拟器 4.3。为了你,我试过了。仍然有同样的错误
-
developers.google.com/maps/documentation/android/reference/com/…。仅当您针对 API 12 及更高版本时,请检查此使用此类。否则,使用 SupportMapFragment .. 为什么没有帮助?您的 min sdk 为 12,在这种情况下您应该使用
MapFragment并扩展Activity。我也将其发布为评论而不是答案。需要更多信息。您是否引用了 google play services library 项目。 -
是的.. 我引用了谷歌播放服务。我希望 minSDKVersion 与 TargetSDKVersion 不同。根据您的评论,我将 MapFragment 与 MinSDKVersion 12 一起使用。正如您所提到的,我通过针对 API 级别 12 及更高级别来使用它。无需将其更改为 11。
-
那么你需要在xml中将
SupportMapFragment更改为MapFragment并且你的Activity类应该扩展Activity。还发布堆栈跟踪