我想在我的应用程序中实现谷歌Map.我得到了MapAPI密钥,并在我的XML文件中使用它,但谷歌Map不显示。当我添加片段到XML文件,它会导致强制关闭错误,当我纠正这个错误,然后谷歌Map在运行时不显示。
MainActivity.java
package com.example.simplemap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
字符串
AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simplemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.example.simplemap.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.simplemap.permission.MAPS_RECEIVE"/>
<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" />
<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.simplemap.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="AIzaSyBVa_dSU5jYR0A0sUFBDgkYIdmD_PO8ADE"/>
</application>
</manifest>
型
activity_main.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"
tools:context=".MainActivity" >
<fragment
android:id="@+id/fragment1"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
型
logcat:
06-16 15:40:09.827: E/Trace(1343): error opening trace file: No such file or directory (2)
06-16 15:40:10.537: D/AndroidRuntime(1343): Shutting down VM
06-16 15:40:10.537: W/dalvikvm(1343): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-16 15:40:10.557: E/AndroidRuntime(1343): FATAL EXCEPTION: main
06-16 15:40:10.557: E/AndroidRuntime(1343): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplemap/com.example.simplemap.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.os.Looper.loop(Looper.java:137)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-16 15:40:10.557: E/AndroidRuntime(1343): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 15:40:10.557: E/AndroidRuntime(1343): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-16 15:40:10.557: E/AndroidRuntime(1343): at dalvik.system.NativeStart.main(Native Method)
06-16 15:40:10.557: E/AndroidRuntime(1343): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.app.Activity.setContentView(Activity.java:1881)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.example.simplemap.MainActivity.onCreate(MainActivity.java:12)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.app.Activity.performCreate(Activity.java:5104)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-16 15:40:10.557: E/AndroidRuntime(1343): ... 11 more
06-16 15:40:10.557: E/AndroidRuntime(1343): 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" />
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.google.android.gms.maps.internal.q.B(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.google.android.gms.maps.internal.q.A(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.google.android.gms.maps.MapFragment$b.gW(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.google.android.gms.dynamic.a.a(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.app.Activity.onCreateView(Activity.java:4716)
06-16 15:40:10.557: E/AndroidRuntime(1343): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-16 15:40:10.557: E/AndroidRuntime(1343): ... 21 more
06-16 15:40:31.647: I/Process(1343): Sending signal. PID: 1343 SIG: 9
型
1条答案
按热度按时间insrf1ej1#
使用以下代码:
片段代码:
字符串
xml代码:
型