gpt4 book ai didi

android - 方向更改后,MapView 的 fragment 在触摸时崩溃

转载 作者:行者123 更新时间:2023-12-02 16:09:43 29 4
gpt4 key购买 nike

我的问题与下面链接中的问题相同,但我无法找到解决方案。并且不知道如何询问作者他的解决方案是什么。 Fragment with map crashes when I change activity and come back to the map

我的应用程序工作正常,直到方向发生变化,直到那时,触摸工作都很好。在方向更改时,它会按应有的方式重新绘制 MapView,但是一旦用户在 map 上执行任何 onTouch 操作,它就会使应用程序崩溃:

android.view.WindowManager$BadTokenException 错误无法添加窗口 --token android.view.ViewRootImpl$blahblahblah 无效。

我正在将 ActionBarSherlock 与 googlemaps 插件一起使用,正如另一个问题所使用的同一教程中所建议的那样。我只是不明白在方向更改后 Activity 重新启动后,我应该如何为 onTouch 事件创建一个新的监听器。我尝试为 onTouch 事件创建一个新的 OnTouchListener,但由于我不确定,并且找不到任何示例,因此我无法猜测再次在 MapView 上获得工作触摸界面的方法。

我确信我缺少一些简单的东西。

我的代码:

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)

最佳答案

在撞墙几天后,我决定将 map 系统升级到Google Maps Android API v2 。这解决了我在使用缩放控件时遇到的问题。

按照以下说明操作后:Using ActionBarSherlock With the New SupportMapFragment

我按照上面的说明创建了一个 SherlockMapFragment,但在我的应用程序包中。我的 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;
}
}

关于android - 方向更改后,MapView 的 fragment 在触摸时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14051800/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com