0

Tengo este problema al implementar un MapView sobre un fragment

public class UbicacionMiEmpresa extends Fragment implements OnMapReadyCallback{
    MapView mapView;
    GoogleMap mGoogleMap;
    View vista;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        vista = inflater.inflate(R.layout.fragment_ubicacion_mi_empresa, container, false);


        return vista;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mapView = (MapView) vista.findViewById(R.id.map_view);
        if(mapView != null){
            mapView.onCreate(null);
            mapView.onResume();
            mapView.getMapAsync(this);

        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        MapsInitializer.initialize(getContext());
        mGoogleMap = googleMap;
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(40.689247, -74.044502)));
        CameraPosition Library = CameraPosition.builder().target(new LatLng(40.689247, -74.044502)).zoom(16).bearing(0).tilt(0).build();
        mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(Library));



    }
}

Mi fragment layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MiEmpresa.UbicacionMiEmpresa">

    <!-- TODO: Update blank fragment layout -->


    <com.google.android.gms.maps.MapView
        android:id="@+id/map_view"
        android:layout_width="278dp"
        android:layout_height="297dp"

        />

</RelativeLayout>

El error al debuggear

Process: mx.emy.emymexico, PID: 9394 java.lang.NullPointerException: Attempt to invoke interface method 'void android.view.inputmethod.InputConnection.closeConnection()' on a null object reference at android.view.inputmethod.InputConnectionWrapper.closeConnection(InputConnectionWrapper.java:270) at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:541) at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6592) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)

Carlos
  • 1
  • me parece dentro del mismo sitio ya existe una solución a ese problema, te sugiero leerlo https://es.stackoverflow.com/questions/42977/cu%C3%A1l-es-la-soluci%C3%B3n-a-todos-los-errores-nullpointerexception-presentes-pasados –  Aug 15 '18 at 23:19

1 Answers1

0

Me parece que te falta inicializar el MapView y que le estas pasando mal al onCreate() el parametro, y deberias llamar todo en onCreateView(); y no en onViewCreated();

public class UbicacionMiEmpresa extends Fragment implements OnMapReadyCallback{
    MapView mapView;
    GoogleMap mGoogleMap;
    View vista;
    Bundle mBundle;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = savedInstanceState;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        vista = inflater.inflate(R.layout.fragment_ubicacion_mi_empresa, container, false);

              try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle this situation
        }


      mapView = (MapView) vista.findViewById(R.id.map_view);
        if(mapView != null){
            mapView.onCreate(mBundle);
            mapView.onResume();
            mapView.getMapAsync(this);

        }
        return vista;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        MapsInitializer.initialize(getContext());
        mGoogleMap = googleMap;
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(40.689247, -74.044502)));
        CameraPosition Library = CameraPosition.builder().target(new LatLng(40.689247, -74.044502)).zoom(16).bearing(0).tilt(0).build();
        mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(Library));



    }
}
Gastón Saillén
  • 3,213
  • 1
  • 7
  • 14