我在Google地图中尝试过一个小例子。我有API密钥,但是当我运行项目时,它会显示以下错误。我还导入了Google Play服务。
我的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.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
我的Java
package com.arun.maps;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;
public class GoogleMapS extends FragmentActivity {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google_map);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.google_map, menu);
return true;
}
}
Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arun.maps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- 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" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name="com.arun.maps.GoogleMapS"
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="
AIzaSyBn3b2eGABl1JL-xXKe1H_uf4UtPRUUAwY" />
</application>
</manifest>
错误:
04-02 17:21:39.355: E/AndroidRuntime(719): FATAL EXCEPTION: main
04-02 17:21:39.355: E/AndroidRuntime(719): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arun.maps/com.arun.maps.GoogleMapS}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
04-02 17:21:39.355: E/AndroidRuntime(719): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.os.Looper.loop(Looper.java:130)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-02 17:21:39.355: E/AndroidRuntime(719): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 17:21:39.355: E/AndroidRuntime(719): at java.lang.reflect.Method.invoke(Method.java:507)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-02 17:21:39.355: E/AndroidRuntime(719): at dalvik.system.NativeStart.main(Native Method)
04-02 17:21:39.355: E/AndroidRuntime(719): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
04-02 17:21:39.355: E/AndroidRuntime(719): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.app.Activity.setContentView(Activity.java:1657)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.arun.maps.GoogleMapS.onCreate(GoogleMapS.java:16)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-02 17:21:39.355: E/AndroidRuntime(719): ... 11 more
04-02 17:21:39.355: E/AndroidRuntime(719): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4323000 but found 0. 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" />
04-02 17:21:39.355: E/AndroidRuntime(719): at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.google.android.gms.maps.internal.q.B(Unknown Source)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.google.android.gms.maps.internal.q.A(Unknown Source)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.google.android.gms.maps.SupportMapFragment$b.gW(Unknown Source)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.google.android.gms.dynamic.a.a(Unknown Source)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
04-02 17:21:39.355: E/AndroidRuntime(719): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
04-02 17:21:39.355: E/AndroidRuntime(719): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
04-02 17:21:39.355: E/AndroidRuntime(719): ... 20 more
答案 0 :(得分:2)
在清单文件的应用程序标记中添加此项。
<!-- Required after latest Google Play Services update -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
答案 1 :(得分:-1)
在应用程序标记内的清单文件中添加:
<meta-data
android:name="com.google.android.gms.version"
android:value="4242000" />