Error reopening Map in Fragment inside Drawerlist

Asked

Viewed 114 times

0

I’m developing an Android app and I’m having a navigation problem between Fragments.

I created a drawerList and in it I have some Ragments, in one of these Ragments I have a map, when I initially access this Ragment, its components are loaded normally including the map, but when selecting another Ragment and back to it, the error happens.

From what I understand the error happens because I try to instantiate a view of Fragment that is already instantiated, but I can’t get the reference of the already instantiated view again to load in Fragment.

Below the code of the Fragment:

    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 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.Fragmentnovoencontro.onCreateView(Fragmentnovoencontro.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, tag null, 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 for your attention

1 answer

0


This is a problem that happens when you use Fragments and the MapFragment. I suggest you replace it with MapView, you won’t have that problem.

But anyway, the solution to your problem is by putting the following line of code in the onCreateView of his 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");
    }


...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.