1

我在这里陷入了一个奇怪的情况,这是偶然发生的。我目前正在使用适用于 Android v2 的 Google Map API 来显示地图片段。一切都运行良好,除了当用户在 USB 模式之间切换时应用程序崩溃。

问题是,我无法保存一些对象,因为在这种情况下我无法覆盖 onSaveInstanceState()。这是 Google Maps API 中的一个问题。我得到Null-Pointer-Exception。完整的logcat如下:

04-27 18:45:10.289: E/AndroidRuntime(19982): FATAL EXCEPTION: main
04-27 18:45:10.289: E/AndroidRuntime(19982): java.lang.RuntimeException: Unable to   start activity ComponentInfo{com.octanetech.cortes/com.octanetech.cortes.MapActivity}:   java.lang.NullPointerException
04-27 18:45:10.289: E/AndroidRuntime(19982):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at android.os.Looper.loop(Looper.java:123)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at android.app.ActivityThread.main(ActivityThread.java:4627)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at java.lang.reflect.Method.invokeNative(Native Method)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at java.lang.reflect.Method.invoke(Method.java:521)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at dalvik.system.NativeStart.main(Native Method)
04-27 18:45:10.289: E/AndroidRuntime(19982): Caused by: java.lang.NullPointerException
04-27 18:45:10.289: E/AndroidRuntime(19982):    at com.octanetech.cortes.MapActivity.displayPlaces(MapActivity.java:644)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at com.octanetech.cortes.MapActivity.onCreate(MapActivity.java:158)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-27 18:45:10.289: E/AndroidRuntime(19982):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-27 18:45:10.289: E/AndroidRuntime(19982):    ... 11 more
04-27 18:45:10.309: W/ActivityManager(2466):   Force finishing activity com.octanetech.cortes/.MapActivity

仅当我恢复地图活动时应用程序才会崩溃,否则应用程序的其余部分工作正常。任何帮助将不胜感激。

编辑添加修剪的 MapActivity 类:

public class MapActivity extends SherlockFragmentActivity implements LocationListener, OnInfoWindowClickListener, SearchView.OnQueryTextListener,
    OnMarkerClickListener {
// private final BitmapDescriptor markerBitmap = null;
private GoogleMap mMap;
private CameraPosition cameraPosition;
Location currentLocation;
MapView mapView;
Bitmap beaconImage;
Geocoder geocoder;
private Handler mHandler;
protected Marker marker;
ActionBar actionBar;
private Menu menu;
private boolean menuCreated;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    navigate = new NavigateActivity(this);
    context = this;
    currentLocation = new Location("asd");
    currentLocation.setLatitude(0);
    currentLocation.setLongitude(0);
    setContentView(R.layout.activity_map);
    mMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();
    SupportMapFragment f = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map));
    f.setRetainInstance(true);

    mMap.setMyLocationEnabled(true);

    mMap.setOnInfoWindowClickListener(this);
    mMap.setOnMarkerClickListener(this);
    cameraPosition = mMap.getCameraPosition();

    geocoder = new Geocoder(this, Locale.getDefault());

    displayPlaces();

}

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    this.menu = menu;
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.mapview_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    mSearchView = (SearchView) searchItem.getActionView();
    setupSearchView(searchItem);
    menuCreated = true;
    return true;
}

private void setupSearchView(MenuItem searchItem) {

    if (isAlwaysExpanded()) {
        mSearchView.setIconifiedByDefault(false);
    } else {
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    }

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if (searchManager != null) {
    }

    mSearchView.setOnQueryTextListener(this);
}

public boolean onQueryTextChange(String newText) {
    // mStatusView.setText("Query = " + newText);
    return false;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.list:
        // app icon in action bar clicked; go home
        Intent intent = new Intent(this, SearchActivity.class);
        intent.removeExtra("search");
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);
        return true;
    case R.id.bookmark:
        intent = new Intent(adasd.this, BookmarkActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

public boolean onQueryTextSubmit(String query) {

    // Extras.createActualDialogueBox(context, "error", url.length()+"");
    // getPlaces(url);
    Intent intent = new Intent(this, LoadingActivityForPlaces.class);
    mBeaconOptionsPoppedUp = false;
    intent.putExtra("key", query);
    startActivity(intent);
    Extras.searched = true;
    return false;
}

public boolean onClose() {

    return false;
}

protected boolean isAlwaysExpanded() {
    return false;
}

@Override
public void onBackPressed() {
    // do nothing.
    if (mBeaconOptionsPoppedUp) {

        mBeaconOptionsPoppedUp = false;
    }
}

@Override
public void onResume() {
    super.onResume();
    displayPlaces();
}

@Override
public void onPause() {
    super.onPause();
    Extras.activityPaused();
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

}

@Override
public void onSaveInstanceState(Bundle bundle) {

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {

}

private void displayPlaces() {
    if (!Extras.trialMode) {
        // TODO Auto-generated method stub
        if (placesMarkers != null) {
            removeMarkers(placesMarkers);
        }
        placesMarkers = new ArrayList<Marker>();
        for (GooglePlace i : Extras.googlePlaces) {
            Marker m = mMap.addMarker(new MarkerOptions()
                    .position(i.getPosition()).title(i.getTitle())
                    .snippet(i.getDescription()));
            if (i.equals(Extras.currentGooglePlace)) {

                m.showInfoWindow();
            }
            placesMarkers.add(m);

        }

        for (GooglePlace i : Extras.cortesPlaces) {
            Marker m = mMap.addMarker(new MarkerOptions()
                    .position(i.getPosition()).title(i.getTitle())
                    .snippet(i.getDescription()));
            if (i.equals(Extras.currentGooglePlace)) {

                m.showInfoWindow();
            }
            placesMarkers.add(m);

        }
    }
}

}

这里的 Extras 是一个带有静态对象的类。访问displayPlaces()函数时应用程序崩溃。当访问Extras.GooglePlaces对象时,会引发空指针异常。我已经在其他一些活动中初始化了这个对象,但是当用户在 USB 模式之间切换时,它会得到 Null,因此会出现空指针异常。

4

1 回答 1

1

您的代码中的ANullPointerException不是"an issue in Google Maps API".

我建议不要使用static除常量、嵌套类和有时工厂方法之外的关键字。这包括单例反模式。

您将不得不使用您的应用程序架构。Chochop 的建议是一个好的开始。您可能想在这里查看我对无关问题的回答中的一些要点:https ://stackoverflow.com/a/16335191/2183804

于 2013-05-07T20:41:13.077 回答