我无法弄清楚我错过了什么。我想在我的主要活动中实现一个简单的谷歌地图。谷歌地图应该只显示在MainActivity中。
Google API密钥已在清单文件中正确更新。我的项目中包含了google-play-services_lib.jar。在Eclipse中创建新的Android项目时,Google API 18被选为“Compile with”。
MainActivity.java中的代码
package com.example.maptest;
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"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
我的AndroidManifest.xml中的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<permission
android:name="com.example.googlemaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<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" />
<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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.maptest.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="MyActualKey" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
Logcat
02-26 22:50:40.075: D/AndroidRuntime(14428): Shutting down VM
02-26 22:50:40.075: W/dalvikvm(14428): threadid=1: thread exiting with uncaught exception (group=0x41465700)
02-26 22:50:40.177: E/AndroidRuntime(14428): FATAL EXCEPTION: main
02-26 22:50:40.177: E/AndroidRuntime(14428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maptest/com.example.maptest.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.os.Looper.loop(Looper.java:137)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-26 22:50:40.177: E/AndroidRuntime(14428): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 22:50:40.177: E/AndroidRuntime(14428): at java.lang.reflect.Method.invoke(Method.java:525)
02-26 22:50:40.177: E/AndroidRuntime(14428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-26 22:50:40.177: E/AndroidRuntime(14428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-26 22:50:40.177: E/AndroidRuntime(14428): at dalvik.system.NativeStart.main(Native Method)
02-26 22:50:40.177: E/AndroidRuntime(14428): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-26 22:50:40.177: E/AndroidRuntime(14428): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.Activity.setContentView(Activity.java:1895)
02-26 22:50:40.177: E/AndroidRuntime(14428): at com.example.maptest.MainActivity.onCreate(MainActivity.java:13)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.Activity.performCreate(Activity.java:5133)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-26 22:50:40.177: E/AndroidRuntime(14428): ... 11 more
02-26 22:50:40.177: E/AndroidRuntime(14428): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
02-26 22:50:40.177: E/AndroidRuntime(14428): ... 21 more
答案 0 :(得分:1)
1)您没有为Google Play服务版本指定元标记。应该有2个元标记 - 您的地图密钥和播放服务版本:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
2)Android Emulator没有安装GooglePlayServices。使用真实设备
<强>更新强>
3)由于您使用支持库中的FragmentActivity
,因此您还必须使用支持地图片段。将activity_main
布局替换为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>