[英]maps api v2 android: Binary XML file line #2 error
I have been toiling with the Google-maps API for Android v2 for days now, and cannot figure out how to get this activity working, here is the layout file activity_main.xml: 我已经使用Android v2的Google-maps API工作了几天,但无法弄清楚如何使此活动正常工作,这是布局文件activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
Here is MainActivity.java: 这是MainActivity.java:
package com.gabe.locator; 包com.gabe.locator;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
And my manifest file: 和我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gabe.locator"
android:versionCode="1"
android:versionName="1.0" >
<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"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- 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-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDW3bQEXEHW956EiLMhgvaXBQMakRa7i6E" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gabe.locator.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>
</manifest>
here is my last error trace: 这是我最后的错误跟踪:
02-06 21:02:57.406: D/AndroidRuntime(29553): Shutting down VM
02-06 21:02:57.406: W/dalvikvm(29553): threadid=1: thread exiting with uncaught exception (group=0x4162a7c0)
02-06 21:02:57.416: E/AndroidRuntime(29553): FATAL EXCEPTION: main
02-06 21:02:57.416: E/AndroidRuntime(29553): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gabe.locator/com.gabe.locator.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.app.ActivityThread.access$600(ActivityThread.java:153)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.os.Looper.loop(Looper.java:137)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.app.ActivityThread.main(ActivityThread.java:5289)
02-06 21:02:57.416: E/AndroidRuntime(29553): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 21:02:57.416: E/AndroidRuntime(29553): at java.lang.reflect.Method.invoke(Method.java:525)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
02-06 21:02:57.416: E/AndroidRuntime(29553): at dalvik.system.NativeStart.main(Native Method)
02-06 21:02:57.416: E/AndroidRuntime(29553): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:320)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.app.Activity.setContentView(Activity.java:1895)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.gabe.locator.MainActivity.onCreate(MainActivity.java:12)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.app.Activity.performCreate(Activity.java:5133)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
02-06 21:02:57.416: E/AndroidRuntime(29553): ... 11 more
02-06 21:02:57.416: E/AndroidRuntime(29553): Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.google.android.gms.maps.internal.q.v(Unknown Source)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.google.android.gms.maps.internal.q.u(Unknown Source)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.google.android.gms.maps.MapFragment$b.eb(Unknown Source)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.google.android.gms.dynamic.a.a(Unknown Source)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
02-06 21:02:57.416: E/AndroidRuntime(29553): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.app.Activity.onCreateView(Activity.java:4745)
02-06 21:02:57.416: E/AndroidRuntime(29553): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
02-06 21:02:57.416: E/AndroidRuntime(29553): ... 20 more
02-06 21:03:00.530: I/Process(29553): Sending signal. PID: 29553 SIG: 9
Does anyone have any idea what is causing the crash each time I run this on Cyanogenmod for HTC One S, with android 4.3.1? 有谁知道每次在Cyanogenmod上使用Android 4.3.1的HTC One S在Cyanogenmod上运行时导致崩溃的原因是什么?
Thanks! 谢谢! Gabe
加布
As per Documentation 根据文档
Note: The Google Maps Android API requires API level 12 or higher for the support of MapFragment objects.
注意: Google Maps Android API需要API级别12或更高版本才能支持MapFragment对象。 If you are targeting an application earlier than API level 12, you can access the same functionality through the SupportMapFragment class.
如果您要定位API级别12之前的应用程序,则可以通过SupportMapFragment类访问相同的功能。 You will also have to include the Android Support Library .
您还必须包括Android支持库 。
Add this both 两者都添加
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDW3bQEXEHW956EiLMhgvaXBQMakRa7i6E" />
Under <application>
tag in manifest.xml
file. 在
manifest.xml
文件中的<application>
标记下。
And extends
your Activity
to FragmentActivity
. 并将您的
Activity
extends
到FragmentActivity
。
Use SupportMapFragment in layout.xml 在layout.xml中使用SupportMapFragment
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
Also extend as FragmentActivity and not as Activity 还扩展为FragmentActivity而不是Activity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.