我的问题与下面链接中的问题相同,但我无法找出解决方案。也不知道如何询问作者他的解决方案。
Fragment with map crashes when I change activity and come back to the map
我的应用程序在屏幕方向改变之前工作得很好,触摸功能也非常流畅。当屏幕方向发生改变时,它会重新绘制MapView,但是一旦用户在地图上进行任何触摸操作,应用程序就会崩溃并显示以下错误信息:
android.view.WindowManager$BadTokenException error Unable to add window --token android.view.ViewRootImpl$blahblahblah is not valid.
我正在使用ActionBarSherlock和谷歌地图插件,这是同一个教程建议使用的。但是我不知道在屏幕方向改变后应该如何创建新的OnTouch事件监听器。我尝试过仅为OnTouch事件创建新的OnTouchListener,但是由于我不确定,并且找不到任何示例,我无法猜测如何再次在MapView上获得可用的Touch接口。
我相信我错过了一些简单的东西。
以下是我的代码:
错误日志包含:
我正在使用ActionBarSherlock和谷歌地图插件,这是同一个教程建议使用的。但是我不知道在屏幕方向改变后应该如何创建新的OnTouch事件监听器。我尝试过仅为OnTouch事件创建新的OnTouchListener,但是由于我不确定,并且找不到任何示例,我无法猜测如何再次在MapView上获得可用的Touch接口。
我相信我错过了一些简单的东西。
以下是我的代码:
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;
}
}
错误日志包含:
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)