I am using the method onRestoreInstanceState
of android to get the information when the screen rotates and it works fine except for one thing.
When i try to set visible a menuItem that have initialized in onCreateOptionsMenu
, it gets NullPointerException
and is very important for the funcionality that the item apearrs
Does anyone know how to fix this?
Thanks a lot for the help
My method to initialize item
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.map, menu);
itemClean = menu.findItem((R.id.map_clear_lines));
return true;
}
My method to recover the info
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(
(LatLng)savedInstanceState.getParcelable(MapConstants.MAP_CAMERA_LATLNG),
savedInstanceState.getFloat(MapConstants.MAP_CAMERA_ZOOM)));
if(savedInstanceState.getBoolean(MapConstants.MAP_TYPE)){
stateSatelitalMap();
}
if(savedInstanceState.containsKey(MapConstants.ROUTES_START)){
LatLng starLatLng = savedInstanceState.getParcelable(MapConstants.ROUTES_START);
LatLng finishLatLng = savedInstanceState.getParcelable(MapConstants.ROUTES_END);
markerStart = mapa.addMarker(new MarkerOptions().position(starLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_inici_fin))
.title(getResources().getString(R.string.marker_confirm_start)).snippet(getResources().getString(R.string.snippet_confirm_start)));
markerFinish = mapa.addMarker(new MarkerOptions().position(finishLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_inici_fin))
.title(getResources().getString(R.string.marker_confirm_end)).snippet(getResources().getString(R.string.snippet_confirm_end)));
getRoute(starLatLng, finishLatLng);
booleanEndConfirmed = true;
booleanStartConfirmed = true;
itemClean.setVisible(true);
}
everything works fine, except the final line, the menuItem don't cause problem when i use it witout rotation the screen