Java Android谷歌地图有时不显示,只显示网格

Java Android谷歌地图有时不显示,只显示网格,java,android,google-maps,Java,Android,Google Maps,所以,我一直在努力让它运行一段时间了。问题是我把谷歌的地图密钥正确地放在main.xml中,但我似乎无法获得提供商信息,因为地图根本不显示。我已经运行了好几次了,结果在同一个地方我们都失败了。我试着调试并注意到返回了一个提供程序字符串,但之后返回了null 代码在以下位置失败: @Override protected void onResume() { super.onResume(); locationManager .requestLocationUp

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

代码在以下位置失败:

@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

    }

}
package com.shawnbe.mallfinder;
导入java.util.List;
导入com.google.android.maps.GeoPoint;
导入com.google.android.maps.MapActivity;
导入com.google.android.maps.MapController;
导入com.google.android.maps.MapView;
导入com.google.android.maps.Overlay;
导入com.google.android.maps.OverlayItem;
导入android.content.Context;
导入android.graphics.drawable.drawable;
导入android.location.Criteria;
导入android.location.location;
导入android.os.Bundle;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MallFinderActivity扩展了MapActivity实现
android.location.LocationListener{
/**在首次创建活动时调用*/
专用地图控制器;
私有地图视图;
私有android.location.LocationManager LocationManager;
专用地质点;
私有位置currentLocation=null;
私有文本视图latituteField;
私有文本视图纵向字段;
私有字符串提供者;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView MapView=(MapView)findViewById(R.id.MapView);
mapView.SetBuilTinZoomControl(真);
位置;
//getBestProvider();
如果(bestProvider!=null)
location=locationManager.getLastKnownLocation(bestProvider);
List mapOverlays=mapView.getOverlays();
Drawable Drawable=this.getResources().getDrawable(
R.可拉拔式标记);
MapItemizedOverlay itemizedoverlay=新MapItemizedOverlay(可绘制,
这),;
LocalGeoPoints loc=新的LocalGeoPoints();
对于(覆盖项a:位置覆盖项){
项目化覆盖。添加覆盖(a);
}
添加(itemizedoverlay);
}
@凌驾
受保护的void onResume(){
super.onResume();
位置经理
.RequestLocationUpdate(getBestProvider(),1000,1,this);
}
public void getLastLocation(){
字符串提供程序=getBestProvider();
currentLocation=locationManager.getLastKnownLocation(提供程序);
if(currentLocation!=null){
setCurrentLocation(当前位置);
}否则{
Toast.makeText(此“尚未获取位置”,Toast.LENGTH\u LONG)
.show();
}
}
public void animateToCurrentLocation(){
如果(currentPoint!=null){
mapController.animateTo(当前点);
}
}
公共字符串getBestProvider(){
locationManager=(android.location.locationManager)getSystemService(Context.location\u服务);
标准=新标准();
标准.设置功率要求(标准.无要求);
标准.设定精度(标准.无要求);
bestProvider=locationManager.getBestProvider(条件,false);
返回最佳提供者;
}
公共无效setCurrentLocation(位置){
int currLatitude=(int)(location.getLatitude()*1E6);
int currLongitude=(int)(location.getLongitude()*1E6);
currentPoint=新的地理点(当前纬度、当前经度);
currentLocation=新位置(“”);
currentLocation.setLatitude(currentPoint.GetLatitude6()/1e6);
currentLocation.setLongitude(currentPoint.getLongitudeE6()/1e6);
}
@凌驾
受保护的布尔值isRouteDisplayed(){
返回false;
}
/*活动暂停时删除locationlistener更新*/
@凌驾
受保护的void onPause(){
super.onPause();
locationManager.RemoveUpdate(此);
}
@凌驾
已更改位置上的公共无效(位置){
intlat=(int)(location.getLatitude());
int lng=(int)(location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(字符串值(lng));
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
Toast.makeText(此“已启用的新提供程序”+提供程序,
吐司。长度(短)。show();
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
Toast.makeText(此“禁用的提供程序”+提供程序,
吐司。长度(短)。show();
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
//TODO自动生成的方法存根
}
}
下面是我的main.xml文件,以及我从谷歌地图注册网站获得的密钥

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


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

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

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)
-官方信息,怎么办

一步一步

1) 创建密钥库

2) 从密钥库生成哈希MD5

3) 在此处登录:

4) 向应用程序添加密钥


祝你好运

如果您有有效的映射密钥,请检查是否正在获取locationManager int的实例
 locationManager = (android.location.LocationManager)getSystemService(Context.LOCATION_SERVICE);
mapView.setCenter(
    new GeoPoint((int)(25.76032 * 1E6),  (int)(-80.213928 * 1E6)));
//Set a reasonable zoom level
mapView.setZoom(12);
protected void onResume() 
{
    super.onResume();
    locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);
}