1

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

Mithun
  • 2,075
  • 3
  • 19
  • 26
  • 2
    Is there any reason why you just dont set a member variable and then use that to set visibility in onPrepareOptionsMenu? – Tim Mutton May 18 '15 at 06:31
  • i wanted to do it directly but that is the solution, in the onRestoreInstanceState setted a global variable and in the onCreateOptionsMenu asked for it and done. Thanks – obed andres gonzalez velez May 18 '15 at 13:34

1 Answers1

0

Following the recomendation of Tim Mutton, i created a global variable and setted in the OnCreateMenuOptions

private boolean booleanItemVisible = false;


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
...
booleanItemVisible = true;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    ...
    itemClean.setVisible(booleanItemVisible);
    return true;
}