我的问题与下面链接中的问题相同,但我无法找出解决方案。并且无法弄清楚如何询问作者他的解决方案是什么。 Fragment with map crashes when I change activity and come back to the map
我的应用程序正常工作,直到方向改变,Touch工作直到那时为止。在方向更改时,它会重新绘制MapView,但只要用户在地图上执行任何onTouch操作,它就会崩溃应用程序:
android.view.WindowManager $ BadTokenException错误无法添加窗口--token android.view.ViewRootImpl $ blahblahblah无效。
我正在使用带有googlemaps插件的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)
答案 0 :(得分:2)
在我的头撞墙几天后,我决定将地图系统升级到Google Maps Android API v2。这解决了我使用缩放控件时遇到的问题。
按照以下说明操作:Using ActionBarSherlock With the New SupportMapFragment
我按照上面的说明创建了一个SherlockMapFragment,但是在我的app包中。我的MapFragment现在扩展了刚刚创建的新SherlockMapFragment。
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;
}
}