0

我有一个我一直在研究的应用程序。主要活动使用适用于 Android (v2) 的 GoogleMap 类显示地图。有时,并非总是如此,我在启动我的应用程序时会得到一个空指针。上次发生这种情况时,它在一天后停止发生,代码没有更改。在查看了 logcat(发布在下面)之后,我有一种预感,地图对象还不可用。

所以我查看了这个页面:https ://developers.google.com/maps/documentation/android/map (验证地图可用性部分),我已经确定地图对象在 onCreate 中不为空。

由于这个问题是间歇性的,我只能猜测它与谷歌服务有关。我最近强制停止并清除了 Google 服务框架应用程序的数据,以尝试更新到 4.2.2(起诉我)。任何想法 - 有没有人听说过这个,或者有没有人知道如何解决它?

编辑:此代码现在再次起作用,正如您在此处看到的那样。我没有改变任何东西。

所以我的问题是:什么可能导致这种行为?我正在使用直接来自 Google Maps for Android v2 文档的代码检查地图对象(下面第 142 行的 NPE)在 onCreate()、onStart() 和 onResume() 中是否为空。

包含在不应允许的 onCreate()、onStart() 和 onResume() 中...:

    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mainMap))
                        .getMap();
    // Check if we were successful in obtaining the map.
    if (mMap != null) {
        // The Map is verified. It is now safe to manipulate the map.

日志猫:

02-14 13:33:03.190: E/AndroidRuntime(5448): Caused by: java.lang.NullPointerException
02-14 13:33:03.190: E/AndroidRuntime(5448):     at maps.ar.b.a(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at maps.y.h.a(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at maps.y.au.a(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at maps.y.ae.moveCamera(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:83)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at android.os.Binder.transact(Binder.java:310)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at com.tyler.ioio.MainActivity.onStart(MainActivity.java:142)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at android.app.Activity.performStart(Activity.java:5114)
02-14 13:33:03.190: E/AndroidRuntime(5448):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
02-14 13:33:03.190: E/AndroidRuntime(5448):     ... 11 more

主要活动:

protected void onStart() {
    super.onStart();

    // This verification should be done during onStart() because the system
    // calls this method when the user returns to the activity, which
    // ensures the desired location provider is enabled each time the
    // activity resumes from the stopped state.
    mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    final boolean gpsEnabled = mLocMan.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!gpsEnabled) {
        new EnableGpsDialogFragment().show(getFragmentManager(),
                "enableGpsDialog");
    }

    // Optimized code to go to location as fast as possible
    Location firstLoc = mLocMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    updateNewFix(getBetterLocation(firstLoc, currentLoc));
    // THIS IS LINE 142 : FORCE CLOSES
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, DEFAULT_ZOOM));
    mMap.setOnMapClickListener(this);
4

1 回答 1

1

我建议您修改您的代码,如下所示;

@Override
protected void onCreate(Bundle savedInstanceState) {

mMap.setOnMapClickListener(this);

}

protected void onStart() {
    super.onStart();

// This verification should be done during onStart() because the system
// calls this method when the user returns to the activity, which
// ensures the desired location provider is enabled each time the
// activity resumes from the stopped state.
    mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    final boolean gpsEnabled = mLocMan.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!gpsEnabled) {
       new EnableGpsDialogFragment().show(getFragmentManager(),
            "enableGpsDialog");
    }


@Override
protected void onResume() {
    super.onResume();
    setup();

private void setup() {

// Optimized code to go to location as fast as possible
Location firstLoc = mLocMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateNewFix(getBetterLocation(firstLoc, currentLoc));
// THIS IS LINE 142 : FORCE CLOSES

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, DEFAULT_ZOOM));

我认为在 onStart() 中添加太多方法是不合适的。

于 2013-02-15T15:28:48.200 回答