我的应用程序在左侧使用ListFragment,用户可以使用它来选择右侧使用的片段.
在排序中,似乎不可能多次显示MapView.第一个问题是它只允许每个Activity有一个MapView实例.
# Exception 1: You are only allowed to have a single MapView in a MapActivity
因此,我将我的MapView和容器保存在Activity类中:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FragmentManager.enableDebugLogging(true); setContentView(R.layout.main); mapViewContainer = LayoutInflater.from(this).inflate(R.layout.maplayout,null); mapView = (MapView) mapViewContainer.findViewById(R.id.map_view); }
但是,这给了我下一个问题:
# Exception 2: The specified child already has a parent. You must call removeView() on the child’s parent first.
((ViewGroup)mapViewContainer).removeView(mapView); ((ViewGroup)mapView.getParent()).removeView(mapView);
得到了NullPointerExeption.
我会很感激任何好的想法,或者如果你能成功做到这一点,你会分享吗?
谢谢 :)
解决方法
是的,也碰到了这个.
不要在片段的XML布局文件中添加MapView.相反,只需在具有id =“@ id / your_map_container_id”的LinearLayout中留下一个位置即可.
在YourMapContainerFragment中声明一个MapView私有成员:
public class YourMapContainerFragment extends Fragment { private MapView mMapView; //...
然后,在YourMapContainerFragment的onCreateView()中这样做:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { // ... Inflate your fragment's layout... // ... if (mMapView == null) { mMapView = new MapView(getActivity(),/*String*/YOUR_MAPS_API_KEY); } else { ((ViewGroup)mMapView.getParent()).removeView(mMapView); } ViewGroup mapContainer = (ViewGroup) fragmentLayout.findViewById(R.id.your_map_container_id); mapContainer.addView(mMapView); // ... }
原文地址:https://www.jb51.cc/android/315418.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。