MapFragment - NullPointerException - Google Maps V2 - Android

自古美人都是妖i 提交于 2019-11-28 14:19:59

Try to check the MapFragment that you've created in the XML of the Activity. Check mine; it's working.

...

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

...

Is yours different? Also, you checked if you put the correct permissions and metadata on the AndroidManifest.xml?

Here's the permission:

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

And here's the metadata section:

<application>

    ...

    <meta-data android:name="com.google.android.maps.v2.API_KEY"
               android:value="HERE GOES YOUR API KEY" />

</application>

I hope this helps you!

MainActivity.xml :

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"/>

MainActivity.java :

private GoogleMap googleMap;
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

You may be missing this from manifest.xml
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!