Google Maps v2 for Android 10 not working even when using SupportMapFragment

1k views Asked by At

I imported google_play_services_lib from the Eclipse extras into my Workspace. I created a new Android Project and included google_play_services_lib as a library. My Project has android-support-v4.jar and google-play-services.jar

Now My Manifest file:

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="16" />
 <uses-permission android:name="android.permission.INTERNET" />

<!-- add for map2 -->
<permission
    android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="MY API NUMBER" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.phanism.gingerbreadmaps.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

My activity_main is:

package com.phanism.gingerbreadmaps;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMap().setMyLocationEnabled(true);
}

}

And my layout file is :

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

When I compile the above code, in emulator, I am being asked to update the Google Play Store. When I press OK, I am getting this error. On a Device, I my App is crashing directly and when I look at the Debugging, this is the error I am getting.

12-03 08:43:06.851: E/AndroidRuntime(620): FATAL EXCEPTION: main
12-03 08:43:06.851: E/AndroidRuntime(620): java.lang.NullPointerException
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1409)
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.app.Activity.startActivityForResult(Activity.java:3351)
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.app.Activity.startActivityForResult(Activity.java:3312)
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:824)
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.app.Activity.startActivity(Activity.java:3522)
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.app.Activity.startActivity(Activity.java:3490)
12-03 08:43:06.851: E/AndroidRuntime(620):  at com.google.android.gms.dynamic.a$5.onClick(Unknown Source)
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.view.View.performClick(View.java:4084)
12-3 08:43:06.851: E/AndroidRuntime(620):   at android.view.View$PerformClick.run(View.java:16966)
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.os.Handler.handleCallback(Handler.java:615)
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.os.Looper.loop(Looper.java:137)
12-03 08:43:06.851: E/AndroidRuntime(620):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-03 08:43:06.851: E/AndroidRuntime(620):  at java.lang.reflect.Method.invokeNative(Native Method)
12-03 08:43:06.851: E/AndroidRuntime(620):  at java.lang.reflect.Method.invoke(Method.java:511)
12-03 08:43:06.851: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-03 08:43:06.851: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-03 08:43:06.851: E/AndroidRuntime(620):  at dalvik.system.NativeStart.main(Native Method)

Can anyone point out to me what I am doing wrong.

5

There are 5 answers

1
Siddharth_Vyas On

Add this in your manifest file :

        <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Hope this helps.

1
TNR On

Hey add the following code as a child element of application tag

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="MY API NUMBER" />

Hope this will help you, if not as @Siddarth Vyas said add the google play services version too then it should work.

EDIT

Also change your xml code for fragment tag

it should be

android:name="com.google.android.gms.maps.SupportMapFragment"

and not

class="com.google.android.gms.maps.SupportMapFragment"

I hope it should solve your problem as that is the only change I see for your code from my working example code.

0
Phaneendra Hari On

I got the answer for this question. The assigning the GoogleMap should be in a if-condition as mentioned below

if(googleMap == null){
        GoogleMapOptions options = new GoogleMapOptions();
        options.compassEnabled(true).mapType(GoogleMap.MAP_TYPE_NORMAL).zoomControlsEnabled(true).rotateGesturesEnabled(true);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        SupportMapFragment.newInstance(options);
        googleMap = mapFragment.getMap();

        if(googleMap != null){
            googleMap.setMyLocationEnabled(true);

            double latitude = 35.1;
            double longitude = 43.9;

            MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("This Works");
            googleMap.addMarker(marker);

        }
    }

Without the if-condition, I am getting a NullPointerException. If I dont add,

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

I am getting a Fragment class error.

0
Harshid On

what about apikey and service version like this

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="YOUR API KEY" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

please put this in your manifest.xml file and re run your code what happen.

please post error message or logcat.

Happy coding!.

0
Delari Jesus On
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.phanism.gingerbreadmaps.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

**<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="MY API NUMBER" />**

</application>

</manifest>