android maps v2 crashes when reopened in fragment

2019-04-06 10:01发布

google maps v2 android loads the first time and then I am switching to other fragments which works when I come back to map fragment it crashes.I have attached the code below :I really appreciate any help .Thanks in Advance.

code for map in 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;
    }
}

code for 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);
                    }

Error:

 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)

6条回答
闹够了就滚
2楼-- · 2019-04-06 10:13

Got the same problem, but fixed after this create class with a public static view there (with a public static flag too).... and inside of your fragment, put this code:

    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 is the public class with theses static variables. this way you will only inflate one time and save the state for recovery in other times.

i dont know if this is a good pratic, but solved my problems.

Sry for my horrible grammar.

查看更多
【Aperson】
3楼-- · 2019-04-06 10:25

My solution for fragment.

// remove current map fragment from stack to prevent crash while reloading

@Override
public void onDetach() {
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.mapView));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();
    super.onDetach();
}
查看更多
等我变得足够好
4楼-- · 2019-04-06 10:26

Just put this code on OnDestroyView()

public void onDestroyView() 
 {
    super.onDestroyView(); 
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();
}
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-04-06 10:26

Try this at the beginning of your map fragment 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) {

    }
查看更多
来,给爷笑一个
6楼-- · 2019-04-06 10:29

In my case, I put this code in onPause(), because I had different fragmaent in the same activity.

@Override
public void onPause() {
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_tab));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();
    super.onPause();
}
查看更多
爷、活的狠高调
7楼-- · 2019-04-06 10:38

I did something like that

View 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;
}
查看更多
登录 后发表回答