Google Maps API v2 Android Studio Map not loading

5k views Asked by At

Please keep in mind that I have read through the hundreds of similar questions such as this but I've seemed to cross off the check list of everything that has been stated in the instructions on how to get the Google maps to start working.

First things first, I'll show my code, then I'll narrate what has been happening.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="elephant.tuskapp"
      android:versionCode="49"
      android:versionName="4.6">

     <uses-sdk android:minSdkVersion="15"/>
     <uses-feature
         android:glEsVersion="0x00020000"
         android:required="true"/>
     <permission android:name="elephant.tuskapp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
     <uses-permission android:name="elephant.tuskapp.permission.MAPS_RECEIVE"/>
     <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
     <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.ACCESS_GPS"/>
     <uses-permission android:name="android.permission.ACCESS_LOCATION"/>
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     <uses-permission android:name="android.permission.CALL_PHONE"/>
     <uses-permission android:name="android.permission.WAKE_LOCK"/>
     <uses-permission android:name="android.permission.SEND_SMS"/>
     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
     <uses-permission android:name="android.permission.CAMERA"/>
     <uses-feature android:name="android.hardware.camera"/>
     <uses-feature android:name="android.hardware.camera.autofocus"/>

     <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
     <uses-permission android:name="android.permission.GET_ACCOUNTS"/>

     <permission android:name="elephant.tuskapp.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
     <uses-permission android:name="elephant.tuskapp.permission.C2D_MESSAGE"/>

     <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

     <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:name=".NVtuskapplication"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

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

       <activity
             android:name=".activity.MainActivity"
             android:label="@string/activity_name">
            <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
         </activity>

         <receiver android:name="elephant.tuskapp.service.AlarmReceiver"></receiver>

    <receiver android:name="elephant.tuskapp.service.TimeSynchronizer">
         <intent-filter>
              <action android:name="android.intent.action.ACTION_TIME_CHANGED"/>
              <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
        </intent-filter>
    </receiver>

    <service android:name=".service.GPSService"></service>
    <service android:name=".service.RouteUpdateService"></service>

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

    <receiver
            android:name=".service.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            <category android:name="elephant.tuskapp"/>
        </intent-filter>
    </receiver>
    <service android:name=".service.GcmIntentService"/>

    <meta-data
        android:name="io.fabric.ApiKey"
        android:value="key" />

    <!-- google maps api v2-->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="key"/>
</application>

Layout 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.MapFragment"/>

Main Activity

package elephant.tuskapp.activity;
import android.app.Activity;
import android.os.Bundle;
import elephant.tuskapp.R;

public class MainActivity extends Activity {

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

When I run my app, I see a blank canvas with the Google logo on the bottom left corner. The Logcat prints out the following error:

-22 20:47:24.822  22720-22720/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: free.elephant.tuskapp, PID: 22720
java.lang.RuntimeException: Unable to start activity ComponentInfo{free.elephant.tuskapp/elephant.tuskapp.activity.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2301)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
      at android.app.ActivityThread.access$800(ActivityThread.java:147)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5232)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:767)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:486)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
      at android.app.Activity.setContentView(Activity.java:2161)
      at elephant.tuskapp.activity.MainActivity.onCreate(MainActivity.java:18)
      at android.app.Activity.performCreate(Activity.java:5984)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2254)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
      at android.app.ActivityThread.access$800(ActivityThread.java:147)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5232)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
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
      at com.google.maps.api.android.lib6.d.fk.a(Unknown Source)
      at com.google.maps.api.android.lib6.a.g.a(Unknown Source)
      at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
      at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
      at com.google.android.gms.maps.internal.i.onTransact(SourceFile:62)
      at android.os.Binder.transact(Binder.java:380)
      at com.google.android.gms.maps.internal.zzc$zza$zza.zzr(Unknown Source)
      at com.google.android.gms.maps.MapFragment$zzb.zzvu(Unknown Source)
      at com.google.android.gms.maps.MapFragment$zzb.zza(Unknown Source)
      at com.google.android.gms.dynamic.zza.zza(Unknown Source)
      at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
      at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
      at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2117)
      at android.app.Activity.onCreateView(Activity.java:5333)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:737)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:486)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
      at android.app.Activity.setContentView(Activity.java:2161)
      at elephant.tuskapp.activity.MainActivity.onCreate(MainActivity.java:18)
      at android.app.Activity.performCreate(Activity.java:5984)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2254)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
      at android.app.ActivityThread.access$800(ActivityThread.java:147)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5232)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

Familiar right? You see it in a lot of other questions. I think what differentiates my question from the others is I may have a problem with different flavors of my app.

To cross out the mistakes others have encountered, I have done the following: - Made sure my api keys are inside the tag of the manifest file. - Ensured that the Google Maps API v2 for Android is enabled on Google Consoles - I'm using the Google Play Services via Gradle (Scroll down for my gradle snippet) - I made sure all of the essential permissions are set - I made sure to enable the glEsVersion.

What I think the problem is?

I think I'm having a problem with my "free.elephant.tuskapp" flavor to authenticate with googles servers. In my gradle build, I maintain two different flavors of my application and I seem to be getting an error with authentication:

Ensure that the "Google Maps Android API v2" is enabled.
Ensure that the following Android Key exists:
API Key: myAPIKEY
Android Application (<cert_fingerprint>;<package_name>): SHA-1;free.elephant.tuskapp

I obtained my API by using the 'elephant.tuskapp' package name rather than the 'free.elephant.tuskapp'

Here's my gradle build if it's of any help: Gradle

   buildscript {
        repositories {
            maven { 
                url 'https://maven.fabric.io/public' 
            }
        }

        dependencies {
            classpath 'io.fabric.tools:gradle:1.+'
        }
    }
    apply plugin: 'com.android.application'
    apply plugin: 'io.fabric'

    repositories {
        maven { 
            url 'https://maven.fabric.io/public' 
        }
    }


    android {
        compileSdkVersion 22
        buildToolsVersion "23.0.0 rc2"

        defaultConfig {
            applicationId "elephant.tuskapp"
            minSdkVersion 15
            targetSdkVersion 15
        }

        productFlavors {
            demo {
                applicationId "demo.elephant.tuskapp"
            }
            free {
                applicationId "free.elephant.tuskapp"
            }
        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    }

    dependencies {
        compile 'com.google.android.gms:play-services:7.5.0'
        compile 'com.google.zxing:core:3.2.0'
        compile('com.crashlytics.sdk.android:crashlytics:2.3.2@aar') {
            transitive = true;
        }
    }

Am I missing out on something? Or does it really have something to do with my flavor builds? If it's the flavors causing trouble, how am I supposed to handle this? I generated new api keys for all my flavors but I don't see how I'm supposed to make that work within the manifest file as well as having it look elegant.

2

There are 2 answers

0
remedy. On BEST ANSWER

Since my app has multiple flavors, I was getting thrown an error that my api-keys were not being validated. The way to resolve this is to ensure that you add all packages of your project into the Google Developer Console.

SHA1;packagename

Then press enter

SHA1;packagename2(flavor)

In my case it was:

SHA1;elephant.tuskapp
SHA1;free.elephant.tuskapp
SHA1;demo.elephant.tuskapp
4
bjiang On

According to your logcat, it says something wrong with your Map fragment layout. Also, I find that there is nothing in your MainActivity, I think you should initial map there. For more details, you can refer code here.

Sample code:

public class MainActivity extends FragmentActivity {

    GoogleMap mGoogleMap;
    private static LatLng MountainView = new LatLng(37.42, -122.08);

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

        // Initial Map
        try {

            if (mGoogleMap == null) {
                mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
        mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
        mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);

        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(MountainView, 15));

        Marker marker = mGoogleMap.addMarker(new MarkerOptions()
                .position(MountainView)
                .title("Have a nice day!"));

    }
}