使用以下代码时,我的Google地图会在意图开始时崩溃。请帮我修复代码。提前谢谢。
MainActivity.java
package com.example.googlemaps;
import info.androidhive.googlemapsv2.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().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();
}
}
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>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.androidhive.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.googlemaps.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.googlemaps.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<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">
<activity
android:name="com.example.googlemaps.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCBZtSAObwgqcDwz7szpyXOxn3tCIhucdU" />
</application>
</manifest>
和Logcat
08-11 07:42:23.320: E/AndroidRuntime(2117): FATAL EXCEPTION: main
08-11 07:42:23.320: E/AndroidRuntime(2117): Process: info.androidhive.googlemapsv2, PID: 2117
08-11 07:42:23.320: E/AndroidRuntime(2117): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.googlemapsv2/com.example.googlemaps.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.os.Looper.loop(Looper.java:136)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-11 07:42:23.320: E/AndroidRuntime(2117): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 07:42:23.320: E/AndroidRuntime(2117): at java.lang.reflect.Method.invoke(Method.java:515)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-11 07:42:23.320: E/AndroidRuntime(2117): at dalvik.system.NativeStart.main(Native Method)
08-11 07:42:23.320: E/AndroidRuntime(2117): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:714)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.app.Activity.setContentView(Activity.java:1929)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.example.googlemaps.MainActivity.onCreate(MainActivity.java:20)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.app.Activity.performCreate(Activity.java:5231)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-11 07:42:23.320: E/AndroidRuntime(2117): ... 11 more
08-11 07:42:23.320: E/AndroidRuntime(2117): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 7571000 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" />
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.google.android.gms.common.GooglePlayServicesUtil.zzaa(Unknown Source)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.google.android.gms.maps.internal.zzy.zzaz(Unknown Source)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.google.android.gms.maps.internal.zzy.zzay(Unknown Source)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.google.android.gms.maps.MapFragment$zzb.zzvu(Unknown Source)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.google.android.gms.maps.MapFragment$zzb.zza(Unknown Source)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.google.android.gms.dynamic.zza.zza(Unknown Source)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
08-11 07:42:23.320: E/AndroidRuntime(2117): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.app.Activity.onCreateView(Activity.java:4785)
08-11 07:42:23.320: E/AndroidRuntime(2117): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
08-11 07:42:23.320: E/AndroidRuntime(2117): ... 21 more
就是这样。我使用eclipse Juno和api是4.4.2 API 19
答案 0 :(得分:1)
在AndroidManifest.xml文件中添加以下MAP_KEY。
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />