我正在尝试使用 map api v2 显示地图。我搜索了与我的问题类似的答案。7 月 23 日,“Android 错误:二进制 XML 文件第 16 行:添加地图片段时错误膨胀类片段。” 我根据答案改变了我的来源。但是在使用地图片段时我仍然遇到问题。任何帮助表示赞赏。谢谢你。
这是我的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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
这是活动中的代码:
package com.example.googlemap;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.*;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="12"
android:targetSdkVersion="16" />
<uses-feature android:required="true"
android:glEsVersion="0x00020000"/>
<permission android:name="com.example.googlemap.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.googlemap.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.googlemap.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>
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="API key" />
</application>
</manifest>
我正在尝试在我的设备上运行它(Andorid 4.1.2,即 api 16)。我在我的项目中添加了 google-play-services-lib。当我运行应用程序时,它会崩溃。这是完整的 logcat 日志:
11-15 17:53:35.785: E/AndroidRuntime(23708): FATAL EXCEPTION: main
11-15 17:53:35.785: E/AndroidRuntime(23708): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemap/com.example.googlemap.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.app.ActivityThread.access$600(ActivityThread.java:138)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.os.Looper.loop(Looper.java:137)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.app.ActivityThread.main(ActivityThread.java:4911)
11-15 17:53:35.785: E/AndroidRuntime(23708): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 17:53:35.785: E/AndroidRuntime(23708): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 17:53:35.785: E/AndroidRuntime(23708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-15 17:53:35.785: E/AndroidRuntime(23708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-15 17:53:35.785: E/AndroidRuntime(23708): at dalvik.system.NativeStart.main(Native Method)
11-15 17:53:35.785: E/AndroidRuntime(23708): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-15 17:53:35.785: E/AndroidRuntime(23708): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:408)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.app.Activity.setContentView(Activity.java:2094)
11-15 17:53:35.785: E/AndroidRuntime(23708): at com.example.googlemap.MainActivity.onCreate(MainActivity.java:13)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.app.Activity.performCreate(Activity.java:5253)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
11-15 17:53:35.785: E/AndroidRuntime(23708): ... 11 more
11-15 17:53:35.785: E/AndroidRuntime(23708): Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Must specify unique android:id, android:tag, or have a parent with an id for com.google.android.gms.maps.MapFragment
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.app.Activity.onCreateView(Activity.java:4882)
11-15 17:53:35.785: E/AndroidRuntime(23708): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
11-15 17:53:35.785: E/AndroidRuntime(23708): ... 21 more