Error when reopening Map in fragment within DrawerList

0

I'm developing an Android Application and am having a problem navigating between Fragments.

I created a drawerList and in it I have some fragments, in one of these fragments I have a map, when initially accessing this fragment, its components are loaded normally including the map, but when selecting another fragment and returning to it, the error happens.

Below the Fragment code:

    public class FragmentNovoEncontro extends Fragment implements LocationListener {
    private GoogleMap map;
    private SupportMapFragment fragment;
    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //try {
            view = inflater.inflate(R.layout.fragment_layout_novo_encontro,
                    container, false);
            ParseUser user = ParseUser.getCurrentUser();
            final String idUser = user.getObjectId();
            final MapFragment mFragm = (MapFragment) getFragmentManager().findFragmentById(R.id.mapOnde);   
            ParseQuery<ParseObject> query = ParseQuery.getQuery("LastLocation");
            query.whereEqualTo("idUser", idUser);
            query.getFirstInBackground(new GetCallback<ParseObject>() {
                public void done(ParseObject object, ParseException e) {
                    ParseGeoPoint point;
                    LatLng frameworkSystemLocation;
                    LastLocationDAO lDAO = new LastLocationDAO();
                    if (object == null) {
                        frameworkSystemLocation = new LatLng(-34.397, 150.644);
                        saveLastLocation(frameworkSystemLocation);
                        // Toast.makeText(FragmentNovoEncontro.this.getActivity(),
                        // "Entrou como nulo", Toast.LENGTH_SHORT).show();
                    } else {
                        point = (ParseGeoPoint) object.get("lastLocation");
                        frameworkSystemLocation = new LatLng(point
                                .getLatitude(), point.getLongitude());
                        saveLastLocation(frameworkSystemLocation);
                        // Toast.makeText(FragmentNovoEncontro.this.getActivity(),
                        // "Pegou a localização", Toast.LENGTH_SHORT).show();
                    }
                    LocationManager locationManager = (LocationManager) FragmentNovoEncontro.this
                            .getActivity().getSystemService(
                                    Context.LOCATION_SERVICE);
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER, 0, 0,
                            FragmentNovoEncontro.this);                                 
                    map = mFragm.getMap();
                    Marker frameworkSystem = map.addMarker(new MarkerOptions()
                            .position(frameworkSystemLocation).title(
                                    "Sua Posição"));
                    // Move a câmera para Framework System com zoom 15.
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                            frameworkSystemLocation, 100));
                    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000,
                            null);
                }
            });
        /*} catch (InflateException e) {
             map is already there, just return view as it is 
            Toast.makeText(FragmentNovoEncontro.this.getActivity(),
                    "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }*/
        return view;
    }

    @Override
    public void onLocationChanged(final Location location) {
        final String idUser = ParseUser.getCurrentUser().getObjectId();
        ParseQuery<ParseObject> query = ParseQuery.getQuery("LastLocation");
        query.whereEqualTo("idUser", idUser);
        query.getFirstInBackground(new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                ParseGeoPoint point;
                LatLng frameworkSystemLocation;
                if (object == null) {
                    frameworkSystemLocation = new LatLng(-34.397, 150.644);
                    saveLastLocation(frameworkSystemLocation);
                    // Toast.makeText(FragmentNovoEncontro.this.getActivity(),
                    // "Pegou Nulo", Toast.LENGTH_SHORT).show();
                } else {
                    point = (ParseGeoPoint) object.get("lastLocation");
                    frameworkSystemLocation = new LatLng(point.getLatitude(),
                            point.getLongitude());
                    if ((location.getLatitude() != frameworkSystemLocation.latitude)
                            && (location.getLongitude() != frameworkSystemLocation.longitude)) {
                        LatLng latLng = new LatLng(location.getLatitude(),
                                location.getLongitude());
                        frameworkSystemLocation = latLng;
                        Marker frameworkSystem = map
                                .addMarker(new MarkerOptions().position(
                                        frameworkSystemLocation).title(
                                        "Marcador"));
                        // Move a câmera para Framework System com zoom 15.
                        map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                                frameworkSystemLocation, 15));
                        map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                        saveLastLocation(frameworkSystemLocation);
                        // Toast.makeText(FragmentNovoEncontro.this.getActivity(),
                        // "Pegou a localização", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    public FragmentNovoEncontro() {

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Add your menu entries here
        super.onCreateOptionsMenu(menu, inflater);
        this.getActivity().getMenuInflater()
                .inflate(R.menu.novo_encontro, menu);
    }

    public void saveLastLocation(LatLng location) {
        ParseGeoPoint point = new ParseGeoPoint(location.latitude,
                location.longitude);
        String idUser = ParseUser.getCurrentUser().getObjectId();
        LastLocationDAO lDAO = new LastLocationDAO();
        lDAO.saveLocation(point, idUser);
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }
}

Below is the error when I do the procedure described above:

  

01-08 10: 27: 37.509: E / AndroidRuntime (31638): FATAL EXCEPTION: main       01-08 10: 27: 37.509: E / AndroidRuntime (31638): Process: com.mobmundo.localmob, PID: 31638       01-08 10: 27: 37.509: E / AndroidRuntime (31638): android.view.InflateException: Binary XML file line # 65: Error   inflating class fragment       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:713)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.view.LayoutInflater.rInflate (LayoutInflater.java:755)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.view.LayoutInflater.rInflate (LayoutInflater.java:758)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.view.LayoutInflater.inflate (LayoutInflater.java:492)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.view.LayoutInflater.inflate (LayoutInflater.java:397)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at com.mobmundo.localmob.activity.FragmentNewEncounter.onCreateView (FragmentNewEncounter.java:45)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.app.Fragment.performCreateView (Fragment.java:1700)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.app.FragmentManagerImpl.moveToState (FragmentManager.java:890)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.app.FragmentManagerImpl.moveToState (FragmentManager.java:1062)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.app.BackStackRecord.run (BackStackRecord.java:684)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:1447)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.app.FragmentManagerImpl $ 1.run (FragmentManager.java:443)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.os.Handler.handleCallback (Handler.java:733)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.os.Handler.dispatchMessage (Handler.java:95)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.os.Looper.loop (Looper.java:136)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.app.ActivityThread.main (ActivityThread.java:5086)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at java.lang.reflect.Method.invoke (Native Method)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:785)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:601)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): Caused by: java.lang.IllegalArgumentException: Binary XML file line # 65:   Duplicate id 0x7f0b0092, null tag, or parent id 0xffffffff with   another fragment for com.google.android.gms.maps.MapFragment       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.app.Activity.onCreateView (Activity.java:4808)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:689)       01-08 10: 27: 37.509: E / AndroidRuntime (31638): ... 18 more

Thanks in advance for your attention

    
asked by anonymous 08.01.2015 / 14:32

1 answer

0

This is a problem that occurs when you use Fragments and MapFragment . I suggest you replace with MapView , you will not have this problem.

But anyway, the solution to your problem is by putting the following line of code in onCreateView of your Fragment , before doing anything else:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

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


...
    
08.01.2015 / 15:44