【问题标题】:android maps v2 crashes when reopened in fragmentandroid maps v2 在片段中重新打开时崩溃
【发布时间】:2013-10-31 13:58:01
【问题描述】:

谷歌地图 v2 android 首次加载,然后我切换到其他片段,当我返回地图片段时它会崩溃。我附上了下面的代码:非常感谢任何帮助。提前致谢。

fragmentMain 中的地图代码:

public class FragmentMain extends Fragment {
    TextView textView;

    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng test = new LatLng(53.551, 9.993);
    private GoogleMap map;

    public FragmentMain() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, null);
        map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

                Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                    .title("Hamburg"));

                Marker test = map.addMarker(new MarkerOptions()
                    .position(test)
                    .title("test")
                    .snippet("test")
                    .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));

                // Move the camera instantly to hamburg with a zoom of 15.
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

                // Zoom in, animating the camera.
                map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

        return view;
    }
}

MainActivity 代码:

  FragmentManager fm = MainActivity.this.getSupportFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    Fragment fragment = null;


                    if(selectedItem.compareTo("second") == 0) {
                        fragment = new FragmentMain();
                    } else if(selectedItem.compareTo("third") == 0) {
                        fragment = new FragmentButton();
                    } else if(selectedItem.compareTo("first") == 0) {
                        fragment = new FragmentCheckBox();
                    } 


                    if(fragment != null) {
                        // Replace current fragment by this new one
                        ft.replace(R.id.activity_main_content_fragment, fragment);


                        ft.commit();
                        // Set title accordingly
                        tvTitle.setText(selectedItem);
                    }

错误:

 FATAL EXCEPTION: main
 android.view.InflateException: Binary XML file line #83: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.mapsfragment.maps.FragmentMain.onCreateView(FragmentMain.java:32)

【问题讨论】:

    标签: android google-maps android-fragments google-maps-android-api-2


    【解决方案1】:

    只需将此代码放在 OnDestroyView() 上

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

    【讨论】:

    • 这会导致 IllegalSTateException,因为我们会在调用 onSaveINstance() 后尝试更改片段的状态!
    • 试试这个 ft.commitAllowingStateLoss();
    • 这是 Api > 11 的问题,你可以在这里看到stackoverflow.com/questions/7469082/…
    【解决方案2】:

    在地图片段 onCreateView 的开头试试这个

    if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        try {
            view = inflater.inflate(R.layout.fragment_main, container, false);
        } catch (InflateException e) {
    
        }
    

    【讨论】:

    • 我是否需要在您的上述代码中添加任何代码。我通过将上述代码粘贴到此代码图上方进行了尝试 = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map )).getMap();它不适用于我刚刚粘贴的代码。
    • 这里你的视图必须声明为静态的
    【解决方案3】:

    在我的例子中,我把这段代码放在 onPause() 中,因为我在同一个活动中有不同的片段。

    @Override
    public void onPause() {
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_tab));  
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
        super.onPause();
    }
    

    【讨论】:

      【解决方案4】:

      遇到了同样的问题,但在此创建类后修复了,那里有一个公共静态视图(也有一个公共静态标志).... 并在您的片段内,输入以下代码:

          View view;
          if(Global.flag==0){ 
                 view = inflater.inflate(R.layout.fragment_main, null);
                 Global.v = view;
                 Global.flag=1;
          }
          else{
             view = Global.v;
         }
      

      ps: Global 是具有这些静态变量的公共类。 这样你只会膨胀一次并保存状态以供其他时间恢复。

      我不知道这是否是一个好的实践,但解决了我的问题。

      为我糟糕的语法道歉。

      【讨论】:

        【解决方案5】:

        我做过类似的事情

        查看 v;

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        
            if (v != null) {
                ViewGroup parent = (ViewGroup) v.getParent();
                if (parent != null) {
                    parent.removeView(v);
                }
            }
        
            try {
                v = inflater.inflate(R.layout.frag_admin_tab1, container, false);
        
                // Getting Map for the SupportMapFragment
                MapFragment mf = (MapFragment) this.getFragmentManager()
                        .findFragmentById(R.id.google_map);
                GoogleMap map = mf.getMap();
                map.setMyLocationEnabled(true);
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        
                TextView tv = (TextView) v.findViewById(R.id.textView_Road);
                tv.setText("Road");
        
                LatLng pnts[] = new LatLng[4];
        
                pnts[0] = new LatLng(0, 0);
                pnts[1] = new LatLng(10, 0);
                pnts[2] = new LatLng(10, 10);
                pnts[3] = new LatLng(0, 10);
        
                PolygonOptions poly = new PolygonOptions().add(pnts)
                        .fillColor(Color.argb(100, 255, 50, 50))
                        .strokeColor(Color.GRAY).strokeWidth(0.5f);
                map.addPolygon(poly);
            } catch (InflateException e) {
            }
        
            v.setBackgroundColor(Color.WHITE);
        
            return v;
        }
        

        【讨论】:

        • 这里你的视图必须声明为静态的
        【解决方案6】:

        我的片段解决方案。

        // 从堆栈中删除当前地图片段以防止重新加载时崩溃

        @Override
        public void onDetach() {
            Fragment fragment = (getFragmentManager().findFragmentById(R.id.mapView));  
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.remove(fragment);
            ft.commit();
            super.onDetach();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多