Java 恢复活动-NullPointerException
我在恢复一个活动时遇到问题,当我打开它时,我的应用程序崩溃了,我得到一个NullPointerException,这很奇怪,因为我有一个=空条件Java 恢复活动-NullPointerException,java,android,nullpointerexception,onresume,Java,Android,Nullpointerexception,Onresume,我在恢复一个活动时遇到问题,当我打开它时,我的应用程序崩溃了,我得到一个NullPointerException,这很奇怪,因为我有一个=空条件 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_maps);//frag setContentView
@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;
}
}您忘了检查mMap
mMap为空。您也不需要在oncreate中调用map,而是通过接口来实现。它可以在任何时候出现,在onResume期间不太可能出现(如never)。您可以查看
帖子!=null
但是mMap
仍然可以为null(这就是发生的情况)
在您的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);
}
哪一行抛出NullPointerException?我编辑了我的问题:错误在第mMap.moveCamera(更新)行上;你在哪里实例化mMap@noev这将停止错误,但应用程序可能不会按照他想要的方式运行。@Khalos是的,我刚刚读了他说“奇怪,因为我有一个!=null条件。”即使他实际上没有检查null。非常感谢Khalos。它是固定的,应用程序不会粉碎和保存以前的相机位置!我是android开发的新手,对此非常兴奋:)也谢谢你的解释!干杯