Android Google MAP API不起作用

时间:2014-07-16 13:18:44

标签: java android google-maps google-maps-api-3 maps

我试图在应用程序中显示地图,但我没有这样做。

我的模拟器运行Google API 19,我将google play服务检查为引用库。

在API控制台上,我启用了Google地图API,但它仍无效。任何帮助表示赞赏!

MainActivity.java:

   public class MainActivity extends Activity {
   @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
    }
   }

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.haber"
    android:versionCode="1"
    android:versionName="1.0" >
<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"/>
<!-- 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"/>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".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.gms.version"
    android:value="@integer/google_play_services_version" />
        <meta-data
    android:name="com.google.android.maps.v2.AIzaSyB0-fA5Q1of75JqI-----------"
    android:value="AIzaSyB0-fA5Q1of75JqIeb0cRAaI---------"/>
    </application>

</manifest>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>

logcat的

07-16 09:11:10.206: D/dalvikvm(1461): GC_FOR_ALLOC freed 127K, 11% free 3546K/3960K, paused 209ms, total 220ms
07-16 09:11:10.366: I/u(1461): Making Creator dynamically
07-16 09:11:10.486: I/Google Maps Android API(1461): Google Play services client version: 5077000
07-16 09:11:10.496: I/dalvikvm(1461): Could not find method guj.a, referenced from method gqi.a
07-16 09:11:10.496: W/dalvikvm(1461): VFY: unable to resolve static method 24932: Lguj;.a (Landroid/content/Context;)Lgri;
07-16 09:11:10.496: D/dalvikvm(1461): VFY: replacing opcode 0x71 at 0x0003
07-16 09:11:10.506: E/dalvikvm(1461): Could not find class 'gpr', referenced from method gps.a
07-16 09:11:10.506: W/dalvikvm(1461): VFY: unable to resolve new-instance 4089 (Lgpr;) in Lgps;
07-16 09:11:10.516: D/dalvikvm(1461): VFY: replacing opcode 0x22 at 0x0000
07-16 09:11:10.516: E/dalvikvm(1461): Could not find class 'gpr', referenced from method gps.a
07-16 09:11:10.516: W/dalvikvm(1461): VFY: unable to resolve new-instance 4089 (Lgpr;) in Lgps;
07-16 09:11:10.516: D/dalvikvm(1461): VFY: replacing opcode 0x22 at 0x0000
07-16 09:11:10.526: E/dalvikvm(1461): Could not find class 'gpr', referenced from method gps.a
07-16 09:11:10.526: W/dalvikvm(1461): VFY: unable to resolve new-instance 4089 (Lgpr;) in Lgps;
07-16 09:11:10.526: D/dalvikvm(1461): VFY: replacing opcode 0x22 at 0x0000
07-16 09:11:10.526: D/dalvikvm(1461): DexOpt: unable to opt direct call 0x5e9f at 0x0a in Lgps;.a
07-16 09:11:10.526: D/dalvikvm(1461): DexOpt: unable to opt direct call 0x5e9f at 0x0c in Lgps;.a
07-16 09:11:10.536: D/dalvikvm(1461): DexOpt: unable to opt direct call 0x5e9f at 0x0a in Lgps;.a
07-16 09:11:10.546: I/Google Maps Android API(1461): Google Play services package version: 5077030
07-16 09:11:10.556: W/dalvikvm(1461): VFY: unable to resolve static field 20868 (t) in Lyp;
07-16 09:11:10.556: D/dalvikvm(1461): VFY: replacing opcode 0x62 at 0x000e
07-16 09:11:10.566: W/dalvikvm(1461): VFY: unable to resolve static field 20868 (t) in Lyp;
07-16 09:11:10.566: D/dalvikvm(1461): VFY: replacing opcode 0x62 at 0x000d
07-16 09:11:10.696: D/AndroidRuntime(1461): Shutting down VM
07-16 09:11:10.696: W/dalvikvm(1461): threadid=1: thread exiting with uncaught exception (group=0xb1af5ba8)
07-16 09:11:10.736: E/AndroidRuntime(1461): FATAL EXCEPTION: main
07-16 09:11:10.736: E/AndroidRuntime(1461): Process: com.example.haber, PID: 1461
07-16 09:11:10.736: E/AndroidRuntime(1461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.haber/com.example.haber.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.os.Looper.loop(Looper.java:136)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at java.lang.reflect.Method.invokeNative(Native Method)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at java.lang.reflect.Method.invoke(Method.java:515)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at dalvik.system.NativeStart.main(Native Method)
07-16 09:11:10.736: E/AndroidRuntime(1461): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.app.Activity.setContentView(Activity.java:1929)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at com.example.haber.MainActivity.onCreate(MainActivity.java:14)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.app.Activity.performCreate(Activity.java:5231)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-16 09:11:10.736: E/AndroidRuntime(1461):     ... 11 more
07-16 09:11:10.736: E/AndroidRuntime(1461): 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
07-16 09:11:10.736: E/AndroidRuntime(1461):     at ozd.a(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at ozd.a(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at ozd.a(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at ozd.a(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at oyf.a(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at ouk.a(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at ouj.a(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at grv.onTransact(SourceFile:107)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.os.Binder.transact(Binder.java:361)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at com.google.android.gms.dynamic.a$4.b(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at com.google.android.gms.dynamic.a.a(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1099)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1201)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:292)
07-16 09:11:10.736: E/AndroidRuntime(1461):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
07-16 09:11:10.736: E/AndroidRuntime(1461):     ... 20 more

3 个答案:

答案 0 :(得分:6)

您的元数据清单标记必须如下:

  <!-- Google Maps API Key -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCIafD5------------" />
    <meta-data

不像名称和值那样是APIKEY值。

答案 1 :(得分:0)

在您的主要活动中尝试在onResume()

中检查Google Play服务
@Override
    public void onResume() {
        super.onResume();


        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        Log.e("playServicesCheck", "status:" + status);
        try {
            if (status != ConnectionResult.SUCCESS) {
                GooglePlayServicesUtil.getErrorDialog(status, this, 1).show();
            }
        } catch (Exception e) {
            Log.e("PLAY SERVE", "error:" + e);
        }

    }

答案 2 :(得分:0)

看起来您的问题已经解决,但我发现您的清单也遗漏了这个:

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

我遇到了一个类似的问题,通过添加它来修复。

我正在用这个撕掉我的头发,我正在处理一个正好访问地图的应用程序,然后突然停止了。我无法看到任何指向失踪许可的东西;当我将清单与另一个使用map api v2的应用程序进行比较时,我才接受它。

我的日志中有一些红色鲱鱼(指示我在这里),我在下面分享它们以防有同样问题的人偶然发现:

  

找不到类&#39; gpr&#39;,从方法gps.a

引用      

类Lcom / google / android / gms / location / internal / ParcelableGeofence的链接;&#39;失败

我仍然感到困惑的是,为什么它之前没有它(谷歌说你需要地图的工作许可)。也许我曾经把它放在那里并且意外地删除了这条线,但无论哪种方式都把它固定下去了。

来源: https://developers.google.com/maps/documentation/business/mobile/android/config