2015-11-29 38 views
0

我在恢復一個活動時遇到了麻煩,當我打開它時我的應用程序崩潰了,而且我得到一個NullPointerException,這很奇怪,因爲我有一個!= null條件。簡歷活動 - 空指針異常

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_maps);//frag 
    setContentView(R.layout.map_view);//linear 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    gotoLocation(51.508182, -0.140315, DEFAULT_ZOOM); 
    //mMap.setMyLocationEnabled(true); 

} 
    @Override 
protected void onResume(){ 
    super.onResume(); 
    MapStateManager mgr = new MapStateManager(this); 
    CameraPosition position = mgr.getSavedCameraPosition(); 
    if(position != null){ 
     CameraUpdate update = CameraUpdateFactory.newCameraPosition(position); 
     mMap.moveCamera(update); 
    } 
} 

錯誤是在線mMap.moveCamera(更新);

MapStateManager.java

公共類MapStateManager {

private static final String LONGITUDE = "longitude"; 
private static final String LATITUDE = "latitude"; 
private static final String ZOOM = "zoom"; 
private static final String BEARING = "bearing"; 
private static final String TILT = "tilt"; 
private static final String MAPTYPE = "MAPTYPE"; 

private static final String PREFS_NAME="mapCameraState";//definition for shared 

private SharedPreferences mapStatePrefs;// save data on the device 

public MapStateManager(Context context){ 
    mapStatePrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
} 

public void saveMapState(GoogleMap map){ 
    SharedPreferences.Editor editor = mapStatePrefs.edit(); 
    CameraPosition position = map.getCameraPosition(); 
    editor.putFloat(LATITUDE, (float) position.target.latitude); 
    editor.putFloat(LONGITUDE, (float) position.target.longitude); 
    editor.putFloat(TILT, position.tilt); 
    editor.putFloat(ZOOM, position.zoom); 
    editor.putFloat(BEARING, position.bearing); 

    editor.commit(); 
} 

public CameraPosition getSavedCameraPosition(){ 
    double latitude = mapStatePrefs.getFloat(LATITUDE, 0); 
    double longitude = mapStatePrefs.getFloat(LONGITUDE, 0); 

    if (latitude==0){ 
     return null; 
    } 
    LatLng target = new LatLng(latitude, longitude); 

    float zoom = mapStatePrefs.getFloat(ZOOM, 0); 
    float bearing = mapStatePrefs.getFloat(BEARING, 0); 
    float tilt = mapStatePrefs.getFloat(TILT, 0); 

    CameraPosition position = new CameraPosition(target, zoom, tilt, bearing); 
    return position; 
} 

}

+1

哪條線被拋出NullPointerException異常? – Khalos

+0

我編輯我的問題:錯誤是在線mMap.moveCamera(更新); –

+0

你在哪裏實例化mMap –

回答

1

您檢查postion != nullmMap仍然可以爲空(這是發生了什麼)。

在您的onResume()方法中,您需要再次獲取地圖,因爲它可能在應用程序處於非活動狀態時丟失。

嘗試這樣:

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    //Maybe move this to an else on the if below? I'm not sure exactly what this does 
    gotoLocation(51.508182, -0.140315, DEFAULT_ZOOM); 

    //Update to the saved position. 
    MapStateManager mgr = new MapStateManager(this); 
    CameraPosition position = mgr.getSavedCameraPosition(); 
    if(position != null){ 
     CameraUpdate update = CameraUpdateFactory.newCameraPosition(position); 
     mMap.moveCamera(update); 
    } 
} 

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

    //Get the map asynchronously again. 

    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 
+1

非常感謝卡洛斯。它是固定的,應用程序不會粉碎並保存以前的CameraPosition!我是在android開發中的新人,真的很興奮:)感謝您的完美!乾杯 –

1

你忘了做一個檢查上MMAP

的MMAP爲空。你也不要在oncreate中調用map,而是通過界面來做。任何時候都可以進入,在onResume期間不太可能(如從未)。