0

Estou desenvolvendo um Aplicativo em Android e estou tendo um problema de navegação entre Fragments.

Criei uma drawerList e nela tenho algumas fragments, em uma dessas fragments tenho um mapa, quando acesso inicialmente essa fragment, os componentes dela são carregados normalmente inclusive o mapa, porem ao selecionar outra fragment e voltar para ela, o erro acontece.

Pelo o que eu entendi o erro acontece porque eu tento instanciar uma view do fragment que já está instanciada, porem não consigo pegar a referencia da view já instanciada novamente para carregar na fragment.

Abaixo o codigo da 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();
    }
}

Abaixo o erro quando faço o procedimento descrito acima:

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

Desde já agradeço a atenção

1 Resposta 1

0

Isso é um problema que acontece quando você utiliza Fragments e o MapFragment. Sugiro que você substitua pelo MapView, você não irá ter esse problema.

Mas, de qualquer forma, a solução para o seu problema é colocando a seguinte linha de código no onCreateView de seu Fragment, antes de fazer qualquer outra coisa:

@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");
    }


...

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .