sgbel2
sgbel2

Reputation: 121

Fragment with MapView crashing on Touch after orientation change

My problem is the same as the one in the link below, but I couldn't figure out the solution. And could not work out how to ask the author what his solution was. Fragment with map crashes when I change activity and come back to the map

My app works fine until the orientation changes, Touch works beautiful up until that point. On orientation change it redraws the MapView as it should, but as soon as the user does any onTouch action on the map it crashes the app with:

android.view.WindowManager$BadTokenException error Unable to add window --token android.view.ViewRootImpl$blahblahblah is not valid.

I'm using ActionBarSherlock with the googlemaps plugin as suggested in the same tutorial the other question used. I just don't get how I'm supposed to create a new listener for the onTouch event after the activity restarts on orientation change. I have tried just creating a new OnTouchListener to the onTouch event but as I'm unsure, and can't find any examples, I haven't been able to guess my way to get a working Touch interface on the MapView again.

I'm sure it's something simple I'm missing.

The code I have:

public MainActivity extends SherlockFragmentActivity {
    private MapView mapView;
    private MapFragment mMapFragment;

    public void onCreate(Bundle savedInstanceState){
        View view = getLayoutInflater().inflate(R.layout.mapLayout, null);
        mapView = (MapView)view.findViewById(R.id.map);
        mapView.setBuiltInZoomControls(true);
        setContentView(R.layout.activity_main);
    }

    public void onResume(){
        setupFragments();
    }

    private void setupFragments(){
        final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        mMapFragment = (MapFragment) getSupportFragmentManager().findFragmentByTag(MapFragment.TAG);
        if (mMapFragment == null){
            mMapFragment = new MapFragment(mapView);
            ft.add(R.id.fragment_container, mMapFragment, MapFragment.TAG);
        }
        ft.show(mMapFragment);
        ft.commit();
    }
}

public class MapFragment extends SherlockFragment{
    public static final String TAG = "MapFragment";
    private MapView mapView;
    private MapOverlay itemizedOverlay;

    public MapFragment(MapView mapView){
        this.mapView=mapView;
    }

    public void onResume(){
        super.onResume();
        List<Overlay> mapOverlays = mapView.getOverlays();

        itemizedOverlay(defaultActiveRoad, mapView.getContext());
        mapOverlays.add(itemizedOverlay);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle savedInstanceBundle){
        super.onCreateView(inflater,vg,savedInstanceBundle);
        return mapView;
    }

    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
    }

    public void onDestroyView(){
        super.onDestroyView();
        ((ViewGroup)mapView.getParent()).removeView(mapView);
    }

    public void setMapView (MapView mapView){
        this.mapView = mapView;
    }

    public MapView getMapView(){
        return mapView;
    }
}

The error log contains

android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@40fab3b0 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:585)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
at android.view.Window$LocalWindowManager.addView(Window.java:547)
at android.widget.ZoomButtonsController.setVisible(ZoomButtonsController.java:371)
at com.google.android.maps.MapView.displayZoomControls(MapView.java:1055)
at com.google.android.maps.MapView$1.onDown(MapView.java:341)
at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:517)
at com.google.android.maps.MapView.onTouchEvent(MapView.java:685)

Upvotes: 3

Views: 1507

Answers (1)

sgbel2
sgbel2

Reputation: 121

After hitting my head against a wall for a few days, I decided to upgrade the map system to Google Maps Android API v2. This has solved the problem I was having with the zoom controls.

After following the instructions on: Using ActionBarSherlock With the New SupportMapFragment

I created a SherlockMapFragment as instructed in the instructions above, but in my app package. My MapFragment now extends the new SherlockMapFragment just created.

public class MapFragment extends SherlockMapFragment {

public static final String TAG = "MapFragment";
private GoogleMap mapView;
private Application global;
public MapFragment(){
}

public void onCreate(Bundle savedInstanceBundle){
    super.onCreate(savedInstanceBundle);
    setRetainInstance(true);
}

public void onResume(){
    super.onResume();

    global = (Application)getSherlockActivity().getApplication();

}

public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle savedInstanceBundle){
    View view=super.onCreateView(inflater, vg, savedInstanceBundle);
    mapView = getMap();
    return view;
}
}

Upvotes: 2

Related Questions