1

Alguien puede ayudarme a solucionar este error de ejecución por favor, Estoy tratando de mostrar un mapa, en un fragmento, estoy trabajando desde la plantilla de un navigationDrawer. He generado un fragment que contiene el siguiente código:

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity extends Fragment implements OnMapReadyCallback {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.map_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
        fragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        LatLng bogota = new LatLng(4.653421, -74.145150);
        googleMap.addMarker(new MarkerOptions().position(bogota)
                .title("Uniagustiniana"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(bogota, 16.1f));
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }

El siguiente es el código map_layout.xml

FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemasandroid.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tecnologias.uniagustinianaapp.MapActivity">

    <fragment android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"></fragment>

/FrameLayout

Los errores en el Android Monitor salen así cuando intento ejecutar este fragment únicamente. El error se refiere a esta linea: fragment.getMapAsync(this);

El error se refiere a esta linea: fragment.getMapAsync(this);

introducir la descripción de la imagen aquí

8
  • Puedes compartir un poco más de información, los xml de las vistas sería una buena opción y señalarnos la línea 34 del activity
    – Dwan Z
    el 31 ago. 2017 a las 14:55
  • @Dwan Z, si está aquí: MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map); fragment.getMapAsync(this); la línea es: fragment.getMapAsync(this); el 31 ago. 2017 a las 15:02
  • que es this? si es de un fragmento, this no debería ser getActivity() ? el 31 ago. 2017 a las 15:05
  • es.stackoverflow.com/questions/97104/…
    – Dwan Z
    el 31 ago. 2017 a las 15:07
  • @Dwan Z ya realice los cambios de esa pregunta pero no logré avanzar, allá tenía otros errores que corregí cambiando el nivel de api a 17 y usando el import.app.fragment, voy a volver mi xml a esa versión y miro que pasa. el 31 ago. 2017 a las 15:13

1 respuesta 1

1

Primeramente noto que tu clase MapActivity en realidad no es una Activity, si no un Fragment, el nombre tal vez no es adecuado.


Me parece que el problema es que estas interpretando mal como cargar un mapa, si cargas en onViewCreated(), en realidad se supone que usarías un MapView y no un MapFragment, como ejemplo revisa esta respuesta:

https://stackoverflow.com/a/19806967/250260


Si usas un MapFragment, usa el método onCreateView() para inicializar el MapFragment (no uses onViewCreated()) :

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.map_layout, container, false);    
         MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
        fragment.getMapAsync(this);

       return rootView;
    }
3
  • 1
    Te agradezco mucho, las respuestas están bien pues a otros le han funcionado voy a crear el proyecto nuevamente y lo revisaré. el 31 ago. 2017 a las 20:27
  • Excelente Alfredo, saludos!
    – Jorgesys
    el 31 ago. 2017 a las 20:34
  • en onCreateView() puedes inicializar el Fragment(map_layout) y el ChildFragment, en ese orden así como lo tienes, o bien inicializas solo el Fragment y en onViewCreated() inicializas el ChildFragment
    – armen
    el 31 ago. 2017 a las 22:02

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.