无法在android中加载地图

时间:2015-03-25 12:55:56

标签: android google-maps google-maps-android-api-2

我想在我的Android应用中使用Google地图,但每次运行应用时都会收到此错误,并且没有地图显示 E / Google Maps Android API:无法加载地图。联系Google服务器时出错这可能是身份验证问题(但可能是由于网络错误)。

我已尝试过互联网上的所有解决方案, 我更改了API_Key,在运行之前卸载了应用程序,添加了所有可能的标签到清单,但谷歌地图没有显示。

当我从互联网上下载样本并让它运行时,但是当我尝试将活动添加到我的原始应用程序时,我得到相同的错误而没有显示地图。

我在manifest,build.gradle和map活动中附加了代码。 我还附上了Logcat和手机的图片

我希望有人能把我放在正确的方向上

感谢

   //Logcat

        03-25 12:18:33.569    1877-1877/com.comp.roadmap.dev I/art﹕ Not late-enabling -Xcheck:jni (already on)
        03-25 12:18:33.917    1877-1889/com.comp.roadmap.dev I/art﹕ Background sticky concurrent mark sweep GC freed 1760(106KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 729KB/729KB, paused 2.270ms total 114.494ms
        03-25 12:18:34.323    1877-1896/com.comp.roadmap.dev D/OpenGLRenderer﹕ Render dirty regions requested: true
        03-25 12:18:34.325    1877-1877/com.comp.roadmap.dev D/﹕ HostConnection::get() New Host Connection established 0xa6d9fa00, tid 1877
        03-25 12:18:34.332    1877-1877/com.comp.roadmap.dev D/Atlas﹕ Validating map...
        03-25 12:18:34.423    1877-1897/com.comp.roadmap.dev D/Exception﹕ java.net.UnknownHostException: Unable to resolve host "webservice.pollsdb.com": No address associated with hostname
            --------- beginning of crash
        03-25 12:18:34.423    1877-1897/com.comp.roadmap.dev E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
            Process: com.comp.roadmap.dev, PID: 1877
            java.lang.RuntimeException: An error occured while executing doInBackground()
                    at android.os.AsyncTask$3.done(AsyncTask.java:300)
                    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                    at java.lang.Thread.run(Thread.java:818)
             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                    at java.io.StringReader.<init>(StringReader.java:47)
                    at com.comp.roadmap.app.login$CheckLoginWSTask.doInBackground(login.java:330)
                    at com.comp.roadmap.app.login$CheckLoginWSTask.doInBackground(login.java:295)
                    at android.os.AsyncTask$2.call(AsyncTask.java:288)
                    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                    at java.lang.Thread.run(Thread.java:818)
        03-25 12:18:34.428    1877-1896/com.comp.roadmap.dev D/﹕ HostConnection::get() New Host Connection established 0xa6d9fd80, tid 1896
        03-25 12:18:34.454    1877-1896/com.comp.roadmap.dev I/OpenGLRenderer﹕ Initialized EGL, version 1.4
        03-25 12:18:34.482    1877-1896/com.comp.roadmap.dev D/OpenGLRenderer﹕ Enabling debug mode 0
        03-25 12:18:34.502    1877-1896/com.comp.roadmap.dev W/EGL_emulation﹕ eglSurfaceAttrib not implemented
        03-25 12:18:34.502    1877-1896/com.comp.roadmap.dev W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6db56e0, error=EGL_SUCCESS
        03-25 12:18:34.573    1877-1896/com.comp.roadmap.dev W/EGL_emulation﹕ eglSurfaceAttrib not implemented
        03-25 12:18:34.573    1877-1896/com.comp.roadmap.dev W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6db5700, error=EGL_SUCCESS
        03-25 12:18:34.734    1877-1889/com.comp.roadmap.dev I/art﹕ Background sticky concurrent mark sweep GC freed 961(70KB) AllocSpace objects, 0(0B) LOS objects, 10% free, 1521KB/1704KB, paused 726us total 123.706ms
        03-25 12:18:34.932    1877-1889/com.comp.roadmap.dev I/art﹕ Background partial concurrent mark sweep GC freed 692(36KB) AllocSpace objects, 0(0B) LOS objects, 25% free, 1492KB/2004KB, paused 1.413ms total 112.221ms
        03-25 12:18:35.308    1877-1877/com.comp.roadmap.dev I/Choreographer﹕ Skipped 41 frames!  The application may be doing too much work on its main thread.
        03-25 12:18:35.815    1877-1877/com.comp.roadmap.dev E/WindowManager﹕ android.view.WindowLeaked: Activity com.comp.roadmap.app.login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{1f1f0dc6 V.E..... R......D 0,0-684,322} that was originally added here
                    at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
                    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
                    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
                    at android.app.Dialog.show(Dialog.java:298)
                    at com.comp.roadmap.app.login$CheckLoginWSTask.onPreExecute(login.java:447)
                    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
                    at android.os.AsyncTask.execute(AsyncTask.java:535)
                    at com.comp.roadmap.app.login.LogMeIn(login.java:282)
                    at com.comp.roadmap.app.login.onCreate(login.java:145)
                    at android.app.Activity.performCreate(Activity.java:5937)
                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                    at android.app.ActivityThread.access$800(ActivityThread.java:144)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:135)
                    at android.app.ActivityThread.main(ActivityThread.java:5221)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:372)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
        03-25 12:18:47.249    1877-1897/com.comp.roadmap.dev I/Process﹕ Sending signal. PID: 1877 SIG: 9
        03-25 12:18:59.919    2114-2126/com.comp.roadmap.dev I/art﹕ Background sticky concurrent mark sweep GC freed 2967(141KB) AllocSpace objects, 2(32KB) LOS objects, 0% free, 1254KB/1254KB, paused 1.542ms total 158.227ms
        03-25 12:19:00.023    2114-2130/com.comp.roadmap.dev D/OpenGLRenderer﹕ Render dirty regions requested: true
        03-25 12:19:00.028    2114-2114/com.comp.roadmap.dev D/﹕ HostConnection::get() New Host Connection established 0xa60ed060, tid 2114
        03-25 12:19:00.046    2114-2114/com.comp.roadmap.dev D/Atlas﹕ Validating map...
        03-25 12:19:00.142    2114-2130/com.comp.roadmap.dev D/﹕ HostConnection::get() New Host Connection established 0xa60a4a60, tid 2130
        03-25 12:19:00.160    2114-2130/com.comp.roadmap.dev I/OpenGLRenderer﹕ Initialized EGL, version 1.4
        03-25 12:19:00.198    2114-2130/com.comp.roadmap.dev D/OpenGLRenderer﹕ Enabling debug mode 0
        03-25 12:19:00.238    2114-2130/com.comp.roadmap.dev W/EGL_emulation﹕ eglSurfaceAttrib not implemented
        03-25 12:19:00.238    2114-2130/com.comp.roadmap.dev W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa60c5200, error=EGL_SUCCESS
        03-25 12:19:00.313    2114-2130/com.comp.roadmap.dev W/EGL_emulation﹕ eglSurfaceAttrib not implemented
        03-25 12:19:00.313    2114-2130/com.comp.roadmap.dev W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa60c5220, error=EGL_SUCCESS
        03-25 12:19:00.653    2114-2114/com.comp.roadmap.dev I/Choreographer﹕ Skipped 34 frames!  The application may be doing too much work on its main thread.
        03-25 12:19:00.732    2114-2126/com.comp.roadmap.dev I/art﹕ Background sticky concurrent mark sweep GC freed 3217(193KB) AllocSpace objects, 0(0B) LOS objects, 10% free, 1609KB/1801KB, paused 12.515ms total 36.928ms
        03-25 12:19:01.149    2114-2130/com.comp.roadmap.dev W/EGL_emulation﹕ eglSurfaceAttrib not implemented
        03-25 12:19:01.149    2114-2130/com.comp.roadmap.dev W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa60c5200, error=EGL_SUCCESS
        03-25 12:19:01.339    2114-2114/com.comp.roadmap.dev I/zzx﹕ Making Creator dynamically
        03-25 12:19:01.372    2114-2114/com.comp.roadmap.dev W/ResourcesManager﹕ Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
        03-25 12:19:01.372    2114-2114/com.comp.roadmap.dev W/ResourcesManager﹕ Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
        03-25 12:19:01.448    2114-2114/com.comp.roadmap.dev I/Google Maps Android API﹕ Google Play services client version: 7095000
        03-25 12:19:01.499    2114-2126/com.comp.roadmap.dev W/art﹕ Suspending all threads took: 48.763ms
        03-25 12:19:01.549    2114-2126/com.comp.roadmap.dev I/art﹕ Background sticky concurrent mark sweep GC freed 126(7KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 2MB/2MB, paused 86.924ms total 168.922ms
        03-25 12:19:01.587    2114-2114/com.comp.roadmap.dev I/Google Maps Android API﹕ Google Play services package version: 7097470
        03-25 12:19:02.333    2114-2126/com.comp.roadmap.dev I/art﹕ Background sticky concurrent mark sweep GC freed 10597(597KB) AllocSpace objects, 1(16KB) LOS objects, 4% free, 11MB/12MB, paused 3.255ms total 206.012ms
        03-25 12:19:02.476    2114-2126/com.comp.roadmap.dev I/art﹕ Background partial concurrent mark sweep GC freed 14992(666KB) AllocSpace objects, 2(1015KB) LOS objects, 15% free, 10MB/12MB, paused 2.601ms total 128.036ms
        03-25 12:19:02.605    2114-2138/com.comp.roadmap.dev E/Google Maps Android API﹕ Authorization failure.  Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
        03-25 12:19:02.664    2114-2138/com.comp.roadmap.dev E/Google Maps Android API﹕ In the Google Developer Console (https://console.developers.google.com)
            Ensure that the "Google Maps Android API v2" is enabled.
            Ensure that the following Android Key exists:
            API Key: AIzaSyDe8mb_u5CJ7D55Z2sohIoy9iT6SP_WM2A
            Android Application (<cert_fingerprint>;<package_name>): A9:DC:CA:C8:90:F6:FA:44:1C:8F:5A:BC:EB:71:57:E8:6C:E5:3B:37;com.comp.roadmap.dev
        03-25 12:19:02.667    2114-2138/com.comp.roadmap.dev I/Google Maps Android API﹕ Failed to contact Google servers. Another attempt will be made when connectivity is established.
        03-25 12:19:02.792    2114-2130/com.comp.roadmap.dev W/EGL_emulation﹕ eglSurfaceAttrib not implemented
        03-25 12:19:02.793    2114-2130/com.comp.roadmap.dev W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa4e7cf00, error=EGL_SUCCESS
        03-25 12:19:04.727    2114-2149/com.comp.roadmap.dev D/﹕ HostConnection::get() New Host Connection established 0x9f4e91f0, tid 2149
        03-25 12:19:04.758    2114-2114/com.comp.roadmap.dev I/Choreographer﹕ Skipped 122 frames!  The application may be doing too much work on its main thread.
        03-25 12:19:05.279    2114-2114/com.comp.roadmap.dev E/WindowManager﹕ android.view.WindowLeaked: Activity com.comp.roadmap.app.login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{f60576c V.E..... R....... 0,0-684,388} that was originally added here
                    at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
                    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
                    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
                    at android.app.Dialog.show(Dialog.java:298)
                    at android.app.AlertDialog$Builder.show(AlertDialog.java:987)
                    at com.comp.roadmap.app.login.Alert(login.java:271)
                    at com.comp.roadmap.app.login.access$300(login.java:36)
                    at com.comp.roadmap.app.login$CheckLoginWSTask.onPostExecute(login.java:378)
                    at com.comp.roadmap.app.login$CheckLoginWSTask.onPostExecute(login.java:295)
                    at android.os.AsyncTask.finish(AsyncTask.java:632)
                    at android.os.AsyncTask.access$600(AsyncTask.java:177)
                    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:135)
                    at android.app.ActivityThread.main(ActivityThread.java:5221)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:372)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
        03-25 12:19:17.719    2114-2150/com.comp.roadmap.dev E/Google Maps Android API﹕ Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).

//Manifest
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.comp.roadmap.app" >

        <permission android:name="com.comp.roadmap.app.permission.MAPS_RECEIVE"
            android:protectionLevel="signature">
        </permission>
        <uses-permission android:name="com.comp.roadmap.app.permission.MAPS_RECEIVE" />

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <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 ACCESS_COARSE/FINE_LOCATION permissions are not required to use
             Google Maps Android API v2, but are recommended.
        -->
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

        <uses-feature android:glEsVersion="0x00020000" android:required="true"></uses-feature>
        <application
            android:name=".PubVar"
            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" >
            </activity>
            <activity
                android:name=".add_msg"
                android:label="@string/title_activity_add_msg" >
            </activity>
            <activity
                android:name=".ItemDetail"
                android:label="@string/title_activity_item_detail" >
            </activity>
            <activity
                android:name=".ListFilter"
                android:label="@string/title_activity_list_filter" >
            </activity>
            <activity
                android:name=".login"
                android:label="@string/title_activity_login" >
                <intent-filter android:label="@string/app_name" >
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".test1"
                android:label="@string/title_activity_test1" >
            </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.API_KEY"
                android:value="AIzaSyDe8mb_u5CJ7D55Z2sohIoy9iT6SP_WM2A" />

            <activity
                android:name=".MapsActivity"
                android:label="@string/title_activity_maps" >
            </activity>
        </application>

    </manifest>

        // build.gradle


        apply plugin: 'android'

        android {
            compileSdkVersion 22
            buildToolsVersion '21.1.2'

            defaultConfig {
                minSdkVersion 11
                targetSdkVersion 19
                versionCode 7
                versionName "0.1.65"
            }
            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
                }
            }

            productFlavors {
                dev {
                    applicationId "com.roadmap.schlogger.dev"
                }

                prod {
                    applicationId "com.roadmapb.schlogger.app"
                }
            }
        }

        dependencies {
            compile fileTree(dir: 'libs', include: ['*.jar'])
            compile files('libs/ftp4j-1.7.2.jar')
            compile files('libs/ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar')
            compile 'com.android.support:appcompat-v7:22.0.0'
            compile 'com.google.android.gms:play-services:7.0.0'

        }

        //MapsActivity.java


        package com.comp.roadmap.app;

        import android.support.v4.app.FragmentActivity;
        import android.os.Bundle;

        import com.google.android.gms.maps.GoogleMap;
        import com.google.android.gms.maps.SupportMapFragment;
        import com.google.android.gms.maps.model.LatLng;
        import com.google.android.gms.maps.model.MarkerOptions;

        public class MapsActivity extends FragmentActivity {

            private GoogleMap mMap; // Might be null if Google Play services APK is not available.

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_maps);
                setUpMapIfNeeded();
            }

            @Override
            protected void onResume() {
                super.onResume();
                setUpMapIfNeeded();
            }

            /**
             * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
             * installed) and the map has not already been instantiated.. This will ensure that we only ever
             * call {@link #setUpMap()} once when {@link #mMap} is not null.
             * <p/>
             * If it isn't installed {@link SupportMapFragment} (and
             * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
             * install/update the Google Play services APK on their device.
             * <p/>
             * A user can return to this FragmentActivity after following the prompt and correctly
             * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
             * have been completely destroyed during this process (it is likely that it would only be
             * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
             * method in {@link #onResume()} to guarantee that it will be called.
             */
            private void setUpMapIfNeeded() {
                // Do a null check to confirm that we have not already instantiated the map.
                if (mMap == null) {
                    // Try to obtain the map from the SupportMapFragment.
                    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                            .getMap();
                    // Check if we were successful in obtaining the map.
                    if (mMap != null) {
                        setUpMap();
                    }
                }
            }

            /**
             * This is where we can add markers or lines, add listeners or move the camera. In this case, we
             * just add a marker near Africa.
             * <p/>
             * This should only be called once and when we are sure that {@link #mMap} is not null.
             */
            private void setUpMap() {
                mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
            }
        }

        //activity_maps.xml

        <fragment 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" android:id="@+id/map"
            tools:context="com.comp.roadmap.app.MapsActivity"
            android:name="com.google.android.gms.maps.SupportMapFragment" />

2 个答案:

答案 0 :(得分:0)

根据您的例外:

  

引起:java.lang.NullPointerException:

     

尝试调用虚方法'int java.lang。 String.length()'对空对象引用             在java.io.StringReader。(StringReader.java:47)

     

at com.comp.roadmap.app.login $ CheckLoginWSTask.doInBackground(login.java:330)

     

at com.comp.roadmap.app.login $ CheckLoginWSTask。 doInBackground(login.java:295)

在行doInBackground(login.java:295)中,您要求的是一个null String变量的长度。

另外,您使用的是哪种API?

  

确保已启用“Google Maps Android API v2”。

  

确保存在以下Android密钥:API密钥:AIzaSyDe8mb_u5CJ7D55Z2sohIoy9iT6SP_WM2A

您下载了API并注册为能够使用maps api?

答案 1 :(得分:0)

如果您使用的是模拟器,可能您没有安装Google Play服务,即使您安装了Google Play服务,也请检查您是否拥有最新版本。