无法在谷歌地图里面切换tabhost

时间:2015-05-10 06:53:56

标签: android google-maps android-fragments android-tabhost

我在两个片段中使用了tabhost。每个片段都包含一个谷歌地图。问题是它在每个片段中都表现良好,但是当我切换回相同的片段时,它会被压碎。
这是错误消息:

05-10 14:40:02.438  19480-19480/com.nthu.test.nthuapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #6: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at com.nthu.test.nthuapp.MapGuiding$TabChild.onCreateView(MapGuiding.java:263)
        ...
 Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f09005b, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
        at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2164)
        ...

我在网上找到了很多解决方案。每个人都告诉我必须删除onDestroy()中的片段,但它仍然不适用于我。

这是我的片段内容:

 View v;

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }            
        if(v!=null){
            ViewGroup parent = (ViewGroup) v.getParent();
            if (parent != null)
                parent.removeView(v);
        }
        /*------------------this is line 263 that caused error----------*/
        v = inflater.inflate(R.layout.allfragment, container, false);
        Bundle args = getArguments();
        if (args != null && args.containsKey(ARGUMENT_NAME)) {
            String text = args.getString(ARGUMENT_NAME);
            if (mMap == null) {
                mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.allmap)).getMap();
            }
            if (mMap != null) {
                if(text.equals("all")){
                    setUpAllMap();
                }
                else if(text.equals("art")){
                    setUpArtMap();
                }
            }



@Override
    public void onDestroyView()
    {
        super.onDestroyView();
        killOldMap();
        mMap = null;
    }
    private void killOldMap() {
        SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.allmap));
        if(mapFragment != null) {
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.remove(mapFragment);
            ft.commit();
        }
    }

我在网上尝试了很多解决方案,但问题仍然存在。有谁可以帮我解决这个问题? 非常感谢!

1 个答案:

答案 0 :(得分:0)

您是否尝试将以下代码添加到地图片段中?也许你省略了什么?如果您没有尝试过,请试一试。

public void onDestroyView() {
    super.onDestroyView();
    FragmentManager fm = getActivity().getSupportFragmentManager();
    Fragment fragment = (fm.findFragmentById(R.id.map));
    FragmentTransaction ft = fm.beginTransaction();
    ft.remove(fragment);
    ft.commit();
}