我的问题与下面链接中的问题相同,但我无法找出解决方案。并且无法弄清楚如何询问作者他的解决方案是什么。 当我更改活动并返回地图时,带有地图的片段崩溃
我的应用程序在方向改变之前运行良好,在此之前 Touch 运行良好。在方向更改时,它会按照应有的方式重绘 MapView,但是一旦用户在地图上执行任何 onTouch 操作,它就会使应用程序崩溃:
android.view.WindowManager$BadTokenException error 无法添加窗口 --token android.view.ViewRootImpl$blahblahblah 无效。
我正在将 ActionBarSherlock 与 googlemaps 插件一起使用,正如其他问题所使用的同一教程中所建议的那样。我只是不明白在活动在方向更改后重新启动后,我应该如何为 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)