【问题标题】:Android googleMap Map does not show, only grid, sometimesAndroid googleMap 地图不显示,只有网格,有时
【发布时间】:2012-08-20 05:48:53
【问题描述】:

所以,我一直在努力让它运行一段时间。问题是我在 main.xml 中正确放置了来自 Google 的地图密钥,但我似乎无法获得提供者信息,因为地图根本不显示。我已经运行了几次,但我们都在同一个地方失败了。我尝试调试并注意到返回了一个提供程序字符串,但是之后返回了一个空值。

代码在

处失败
@Override
protected void onResume() {
    super.onResume();
    locationManager
            .requestLocationUpdates(getBestProvider(), 1000, 1, this);
}

带有以下错误消息。

08-20 05:14:43.573: E/AndroidRuntime(239): Uncaught handler: thread main exiting due to uncaught exception
08-20 05:14:43.583: E/AndroidRuntime(239): java.lang.RuntimeException: Unable to resume activity {com.shawnbe.mallfinder/com.shawnbe.mallfinder.MallFinderActivity}: java.lang.NullPointerException
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2950)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2965)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2516)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.os.Looper.loop(Looper.java:123)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.main(ActivityThread.java:4363)
08-20 05:14:43.583: E/AndroidRuntime(239):  at java.lang.reflect.Method.invokeNative(Native Method)
08-20 05:14:43.583: E/AndroidRuntime(239):  at java.lang.reflect.Method.invoke(Method.java:521)
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-20 05:14:43.583: E/AndroidRuntime(239):  at dalvik.system.NativeStart.main(Native Method)
08-20 05:14:43.583: E/AndroidRuntime(239): Caused by: java.lang.NullPointerException
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.shawnbe.mallfinder.MallFinderActivity.onResume(MallFinderActivity.java:61)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.Activity.performResume(Activity.java:3763)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2937)
08-20 05:14:43.583: E/AndroidRuntime(239):  ... 12 more

有时,代码似乎启动了网格但没有地图。在其他运行中,它只是失败了。

以下是我的活动代码。

package com.shawnbe.mallfinder;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MallFinderActivity extends MapActivity implements
        android.location.LocationListener {
    /** Called when the activity is first created. */

    private MapController mapController;
    private MapView mapView;
    private android.location.LocationManager locationManager;
    private GeoPoint currentPoint;
    private Location currentLocation = null;
    private TextView latituteField;
    private TextView longitudeField;
    private String bestProvider;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        Location location;
        //getBestProvider();
        if(bestProvider !=null)
        location = locationManager.getLastKnownLocation(bestProvider);

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(
                R.drawable.androidmarker);
        MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable,
                this);
        LocalGeoPoints loc = new LocalGeoPoints();
        for (OverlayItem a : loc.overlayitems) {
            itemizedoverlay.addOverlay(a);
        }
        mapOverlays.add(itemizedoverlay);

    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager
                .requestLocationUpdates(getBestProvider(), 1000, 1, this);
    }

    public void getLastLocation() {
        String provider = getBestProvider();
        currentLocation = locationManager.getLastKnownLocation(provider);
        if (currentLocation != null) {
            setCurrentLocation(currentLocation);
        } else {
            Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG)
                    .show();
        }
    }

    public void animateToCurrentLocation() {
        if (currentPoint != null) {
            mapController.animateTo(currentPoint);
        }
    }

    public String getBestProvider() {
        locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
        criteria.setAccuracy(Criteria.NO_REQUIREMENT);
        bestProvider = locationManager.getBestProvider(criteria, false);

        return bestProvider;
    }

    public void setCurrentLocation(Location location) {
        int currLatitude = (int) (location.getLatitude() * 1E6);
        int currLongitude = (int) (location.getLongitude() * 1E6);
        currentPoint = new GeoPoint(currLatitude, currLongitude);
        currentLocation = new Location("");
        currentLocation.setLatitude(currentPoint.getLatitudeE6() / 1e6);
        currentLocation.setLongitude(currentPoint.getLongitudeE6() / 1e6);
    }

    @Override
    protected boolean isRouteDisplayed() {

        return false;
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

下面是我从谷歌地图注册站点获得的密钥的 main.xml ccomplete。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <FrameLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="0steid872FeGgae0eO2Dhdyei37sFPHG65t7N6XA"/>
    </FrameLayout>
</LinearLayout>

【问题讨论】:

  • See this link 了解更多信息。
  • 我已经正确设置了密钥。
  • 我认为这个错误只会因为错误的密钥而发生。
  • 但我拥有使用我签署应用程序时的 MD5 指纹生成的正确密钥。除了在运行之前将其添加到 main.xml 文件之外,我还需要做什么?
  • 请删除您立即发布的 Google Maps API 密钥。人们很容易滥用它。

标签: java android google-maps


【解决方案1】:

https://developers.google.com/maps/documentation/android/mapkey - 官方信息,怎么办。

一步一步来。

1) 创建密钥库

2) 从您的密钥库生成哈希 MD5

3) 在此登录:https://developers.google.com/android/maps-api-signup

4) 向您的应用程序添加密钥

祝你好运!

【讨论】:

  • 到目前为止,我已经这样做了大约 5 次,但仍然没有。我有 samd MD5 Hash 并且仍然使用相同的谷歌密钥,但它是相同的
【解决方案2】:

onCreate 中,您应该初始化locationManager 字段。移动

locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);

getBestProvider() 方法到你的 onCreate() 的字符串

查看您的堆栈跟踪。你只有一个地方可能发生 NPE

. . . 
08-20 05:14:43.583: E/AndroidRuntime(239): Caused by: java.lang.NullPointerException
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.shawnbe.mallfinder.MallFinderActivity.onResume(MallFinderActivity.java:61)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)

【讨论】:

  • 如果您查看那里的代码示例,您会发现我已经尝试在 Oncreate() 方法中实例化 locationManager 对象
【解决方案3】:

您获得 NullPointer 的原因是您在使用 onResume() 之前从未设置过 locationManager

onCreate() 将在您将 getBestProvider() 注释掉的地方被调用。然后onResume() 被调用并使用locationManager。虽然你这里还有一个更复杂的情况。我会看看我是否可以在程序上遍历 onResume() 中将执行哪些代码...

  1. super.onResume()
  2. locationManager.requestLocationUpdates()
  3. locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);

如果我是你,我会简化一些事情,只在onCreate() 中设置 locationManager(作为一般编程实践,你想限制方法中副作用的数量,我的意思是在getBestProvider() 中,你应该只需返回最好的提供者,并尽量不要对您的状态做任何其他事情,否则您会在该方法之外得到疯狂的意外事情)。如果你还在发疯,我会把getBestProvider() 的第一行放在onResume()

protected void onResume() 
{
    super.onResume();
    locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);
}

您可能不想这样,但它会确保您的问题是onResume() 中的 locationManager 为空

祝你好运。

【讨论】:

    【解决方案4】:

    如果你有有效的地图键,那么检查你是否将 locationManager 的实例放入 onResume 我猜你必须在 onResume() 中实例化位置管理器。

     locationManager = (android.location.LocationManager)getSystemService(Context.LOCATION_SERVICE);
    

    【讨论】:

    • here 是获取地图api密钥的教程。
    【解决方案5】:

    您需要将地图视图居中并设置合理的缩放级别。否则,地图视图可能会显示一些随机区域。

    mapView.setCenter(
        new GeoPoint((int)(25.76032 * 1E6),  (int)(-80.213928 * 1E6)));
    //Set a reasonable zoom level
    mapView.setZoom(12);
    

    【讨论】:

      【解决方案6】:

      NullPointerException 是因为您正在访问 locationmanager 的 null 对象。

      【讨论】:

        【解决方案7】:

        在发布模式下创建apk并运行这个apk文件,因为当apk处于发布模式时会显示谷歌地图。

        【讨论】:

          【解决方案8】:

          请更新您的 Google 地图 API 密钥,之前的 API 密钥已过期,因此在这种情况下您无法看到 Google 地图。 https://developers.google.com/android/maps-api-signup

          【讨论】:

            【解决方案9】:

            确保在使用前对其进行初始化。

            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            

            【讨论】:

            • locationManager 在您尝试使用它的一些地方为空。确保在这些地方(靠近 onCreate() 的顶部)使用它之前对其进行初始化。此外,您获得网格是因为您可能没有互联网连接来下载实际地图......确保您的清单也授予互联网权限!
            【解决方案10】:

            可能还有一个mapkey问题:

            如果您在调试模式下运行应用程序进行测试,则必须使用默认密钥库创建一个映射键以进行调试(例如在 linux 上,您可以在 ~/.android/debug.keystore 找到它)并将此映射键插入代码并使用它,直到你调试你的应用程序。

            如果您想创建一个 apk 并运行它(例如在市场上发布您的 apk),您必须使用您的个人密钥库创建一个新的 mapkey,并在生成 apk 之前从您的代码中更改它

            【讨论】:

              【解决方案11】:

              MAP API 需要有效的密钥才能正常工作。

              要获取应用程序的 api 密钥,您需要按照以下步骤操作。

              1. 使用 keytool 命令生成应用程序的认证指纹。

                keytool -list -keystore ~/.android/debug.keystore 示例:94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98

              2. 使用指纹值,您可以从下面给出的链接生成地图 api 密钥

                https://developers.google.com/android/maps-api-signup

              注意:

              1. 当你想释放应用程序时,不能使用同一个maps api key。您必须使用发布密钥库而不是调试密钥库 (.android/debug.keystore)。使用上面给出的命令生成指纹,并使用它来生成地图 api 密钥以供生产使用。
              2. keytool 二进制文件可以在您的系统 jre bin 文件夹中找到。

              C:\Program Files\Java\jdk1.7.0_06\jre\bin

              【讨论】:

                【解决方案12】:

                locationManager 为空,没有讨论,你在 manifest.xml 中指定了吗?

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-09-23
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多