FATAL EXCEPTION: main java.lang.NullPointerException Android Studio

1

Can someone help me solve this execution error please, I'm trying to show a map, in a fragment, I'm working from the template of a navigationDrawer. I have generated a fragment that contains the following code:

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

The following is the code 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

The errors in the Android Monitor come out this way when I try to execute this fragment only. The error refers to this line: fragment.getMapAsync (this);

    
asked by Ivan Alfredo 31.08.2017 в 16:51
source

1 answer

1

Firstly I notice that your class MapActivity is not really a Activity , but a Fragment , the name maybe not suitable.

I think the problem is that you are misreading how to load a map, if you load in onViewCreated(), you are really supposed to use a MapView and not a MapFragment , as an example check this answer:

link

If you use a MapFragment , use the onCreateView() method to initialize the MapFragment (do not use 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;
    }
    
answered by 31.08.2017 / 21:01
source