0

那是我的代码!

活动主

package com.example.googlemap;

import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity
{
   @Override
   protected void onCreate(Bundle arg0)
   {
      // TODO Auto-generated method stub        
      super.onCreate(arg0);
      setContentView(R.layout.activity_main);

      SupportMapFragment fragment = new SupportMapFragment();
      getSupportFragmentManager().beginTransaction().add(android.R.id.content, fragment).commit();
   }
}

主要的.xml

<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:map="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context=".MainActivity" >    <fragment
           android:id="@+id/map"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           class="com.google.android.gms.maps.SupportMapFragment"
           /> </RelativeLayout>

显现

 <?xml version="1.0" encoding="utf-8"?> <manifest
 xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.googlemap"
     android:versionCode="1"
     android:versionName="1.0" >

     <uses-sdk
         android:minSdkVersion="8"
         android:targetSdkVersion="18" />

     <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"/>

     <application
         android:allowBackup="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" >
         <activity
             android:name="com.example.googlemap.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>

     <meta-data
         android:name="com.google.android.maps.v2.API_KEY"
         android:value="AIzaSyAlBNf0GETrO0RdifpWH39ztBR7Cz1iyzE" />

 </manifest>

和我的日志猫

09-25 14:19:32.644: D/dalvikvm(2152): GC_CONCURRENT freed 154K, 2%
 free 16455K/16636K, paused 2ms+1ms, total 5ms 09-25 14:19:32.660:
 D/AndroidRuntime(2152): Shutting down VM 09-25 14:19:32.660:
 W/dalvikvm(2152): threadid=1: thread exiting with uncaught exception
 (group=0xa6208908) 09-25 14:19:32.664: E/AndroidRuntime(2152): FATAL
 EXCEPTION: main 09-25 14:19:32.664: E/AndroidRuntime(2152):
 java.lang.RuntimeException: Unable to start activity
 ComponentInfo{com.example.googlemap/com.example.googlemap.MainActivity}:
 android.view.InflateException: Binary XML file line #11: Error
 inflating class fragment 09-25 14:19:32.664: E/AndroidRuntime(2152):
    at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.app.ActivityThread.access$600(ActivityThread.java:141) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.os.Handler.dispatchMessage(Handler.java:99) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 android.os.Looper.loop(Looper.java:137) 09-25 14:19:32.664:
 E/AndroidRuntime(2152):    at
 android.app.ActivityThread.main(ActivityThread.java:5041) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 java.lang.reflect.Method.invokeNative(Native Method) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 java.lang.reflect.Method.invoke(Method.java:511) 09-25 14:19:32.664:
 E/AndroidRuntime(2152):    at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 dalvik.system.NativeStart.main(Native Method) 09-25 14:19:32.664:
 E/AndroidRuntime(2152): Caused by: android.view.InflateException:
 Binary XML file line #11: Error inflating class fragment 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 android.view.LayoutInflater.inflate(LayoutInflater.java:489) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 android.view.LayoutInflater.inflate(LayoutInflater.java:396) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 android.view.LayoutInflater.inflate(LayoutInflater.java:352) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.app.Activity.setContentView(Activity.java:1881) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 com.example.googlemap.MainActivity.onCreate(MainActivity.java:16)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.app.Activity.performCreate(Activity.java:5104) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    ... 11 more 09-25
 14:19:32.664: E/AndroidRuntime(2152): Caused by:
 java.lang.RuntimeException: API key not found.  Check that <meta-data
 android:name="com.google.android.maps.v2.API_KEY" android:value="your
 API key"/> is in the <application> element of AndroidManifest.xml
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at maps.ag.bb.a(Unknown
 Source) 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 maps.ag.bb.a(Unknown Source) 09-25 14:19:32.664:
 E/AndroidRuntime(2152):    at maps.ag.an.a(Unknown Source) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at maps.ag.bh.a(Unknown Source)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at maps.ag.bg.a(Unknown
 Source) 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 bob.onTransact(SourceFile:107) 09-25 14:19:32.664:
 E/AndroidRuntime(2152):    at
 android.os.Binder.transact(Binder.java:310) 09-25 14:19:32.664:
 E/AndroidRuntime(2152):    at
 com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown
 Source) 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown
 Source) 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 com.google.android.gms.dynamic.a$4.b(Unknown Source) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 com.google.android.gms.dynamic.a.a(Unknown Source) 09-25 14:19:32.664:
 E/AndroidRuntime(2152):    at
 com.google.android.gms.dynamic.a.onCreateView(Unknown Source) 09-25
 14:19:32.664: E/AndroidRuntime(2152):  at
 com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown
 Source) 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    at
 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
 09-25 14:19:32.664: E/AndroidRuntime(2152):    ... 21 more
4

2 回答 2

0

在我看来,meta-data带有 API 密钥的标签应该放在该application部分内。你的就在它外面。

我在之前的两个项目中使用过 Maps v2,并在applicationXML 元素中添加了 API 密钥。

于 2013-09-25T07:47:12.017 回答
0

在这里查看:https ://developers.google.com/maps/documentation/android/start#adding_the_api_key_to_your_application

您的 API 密钥应是元素的子application元素。

于 2013-09-25T07:46:51.047 回答