Cannot fix map fragment in fragment NullPointerException

I am trying to show Map in Fragment but I always get this NullPointerException:

java.lang.NullPointerException: Attempt to call virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap ()' on null reference to object

I knew there are many questions with this exception in this case, but I cannot find my error. I have read any answer I have found.

Here is my code:

public class DetailKarteFragment extends Fragment {

GoogleMap map;

static final LatLng brend = new LatLng(48.079, 8.157);

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_detail_karte, null, false);

    FragmentTransaction ft = getFragmentManager().beginTransaction();

    map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    SupportMapFragment fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);

    if (fmap == null) {
        fmap = SupportMapFragment.newInstance();
        ft.add(R.id.map, fmap);
    }

    ft.commit();

    Marker brendMarker = map.addMarker(new MarkerOptions().position(brend).title("Brend"));

    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(brend, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

    return v;
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}
}

      

and xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@color/background"
    android:layout_height="match_parent">


<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Route anzeigen"
        android:id="@+id/button_route_planen"
        android:layout_gravity="center_horizontal"
        android:height="48dp"
        android:background="@color/secondary_100"
        android:layout_marginBottom="-48dp"/>

</LinearLayout>

</LinearLayout>

      

+3


source to share


3 answers


Just move the line map = (...).getMap()

after ft.commit()

. You are getting this exception because you FragmentManager

cannot find a chunk that has not yet been add

ed or replace

d in the container.

The best solution in my opinion would be:



SupportMapFragment fmap;
fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);

if (fmap == null) {
    fmap = SupportMapFragment.newInstance();
    ft.add(R.id.map, fmap);
    ft.commit();
}
map = fmap.getMap();

      

0


source


You fetch the feature map GoogleMap

before checking if it exists SupportMapFragment

. I am assuming this snippet has not been added yet and you are referencing the method getMap()

on a null reference.



0


source


I know this question is very old ... But I had this problem and would like to share what is happening with me and the solution.

I tried to put a MapFragment inside a fragment just like you do. But I forgot to put the key in my manifest:

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

      

So when this happens in Activity, it will throw an exception. This is great because your application crashes and you can read the log and figure out what you need to do! You will receive this message:

    Caused by: java.lang.RuntimeException: API key not found. 
     Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> 
is in the <application> element of AndroidManifest.xml

      

It's very easy to find what needs to be done this way, right?

But this doesn't happen when the MapFragment is inside another fragment!

It only returns the zero chunk ... just like the problem you are currently having.

So just put the key in your manifest and you should be fine! Hopefully google will fix this because it took me hours to find out what was wrong.

0


source







All Articles