简体   繁体   English

Android Google MAP API不起作用

[英]Android Google MAP API does not work

I'm trying to show a map in the application, but I'm failing to do so. 我正在尝试在应用程序中显示地图,但没有这样做。

My emulator runs the Google API 19 and I checked the google play services as referenced library. 我的模拟器运行Google API 19,并检查了Google Play服务作为参考库。

On API Console I switched the Google map API on, but it's still not working. 在API控制台上,我打开了Google map API,但仍无法正常工作。 Any help is appreciated! 任何帮助表示赞赏!

MainActivity.java: MainActivity.java:

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

Manifest file 清单文件

<?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 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 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

Your meta-data manifest tag must be like this: 您的元数据清单标签必须像这样:

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

Not the APIKEY value in name and value, as you have. 不是像名称那样具有名称和值中的APIKEY值。

in your main activity try checking for google play services in onResume() 在您的主要活动中,尝试在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);
        }

    }

Looks like your issue has been solved, but I've noticed your manifest is also missing this: 看来您的问题已解决,但我注意到您的清单也缺少此信息:

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

I was having a similar issue that was fixed by adding this. 我有一个类似的问题,可以通过添加此问题来解决。

I was tearing my hair out with this, I'm working on an application that was accessing maps just fine, then stopped out of the blue. 我当时正在扯头发,我正在开发的应用程序可以正常访问地图,然后突然停止运行。 I couldn't see anything actually pointing to a missing permission; 我看不到任何实际上指向缺少权限的内容; I only picked up on it when I compared the manifest with another application that used maps api v2. 我将清单与另一个使用maps api v2的应用程序进行比较时才了解它。

My log had a couple of red herrings (that directed me here), I'm sharing them below in case someone with the same issue stumbles across them: 我的日志上有几条红色的鲱鱼(将我引到这里),我在下面共享它们,以防遇到相同问题的人偶然发现它们:

Could not find class 'gpr', referenced from method gps.a 找不到方法gps.a引用的类'gpr'

Link of class Lcom/google/android/gms/location/internal/ParcelableGeofence;' Lcom / google / android / gms / location / internal / ParcelableGeofence类的链接;' failed 失败了

I'm still confused as to why it previously worked without it (Google says you need the permission for maps to work). 对于为什么以前没有它,我仍然感到困惑(Google表示您需要获得地图的许可才能工作)。 Perhaps I'd once put it there and accidentally deleted the line, but either way putting it in is what fixed it. 也许我曾经把它放在那儿,然后不小心删除了那条线,但是把它放进去的任何一种方式都可以解决它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM