当在片段中重新打开时,android maps v2崩溃

jas*_*son 9 android google-maps android-fragments google-maps-android-api-2

谷歌地图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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
                    }
Run Code Online (Sandbox Code Playgroud)

错误:

 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)
Run Code Online (Sandbox Code Playgroud)

Har*_*dik 11

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

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


Bre*_*bin 5

在地图片段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) {

    }
Run Code Online (Sandbox Code Playgroud)