2

My application worked yesterday, I cant figure out why it stopped working. The only thing I tried doing was to create a signed apk. to do this I had to create a new Google maps key and place it into the manifest file. I done this following the Google developers tutorial. But now when I try to run the app it start up, I click a button and it force closes.

This is the error log:

  03-20 23:31:33.636  10036-10036/project.sharethefare E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: project.sharethefare, PID: 10036
        java.lang.RuntimeException: Unable to start activity ComponentInfo{project.sharethefare/project.sharethefare.CurrentLocation}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
                at android.app.ActivityThread.access$900(ActivityThread.java:172)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5834)
                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:1388)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
         Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
                at android.app.Activity.setContentView(Activity.java:2241)
                at project.sharethefare.CurrentLocation.onCreate(CurrentLocation.java:24)
                at android.app.Activity.performCreate(Activity.java:6221)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
                at android.app.ActivityThread.access$900(ActivityThread.java:172)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5834)
                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:1388)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
         Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.maps.v2.API_KEY in the <application> element of AndroidManifest.xml
                at com.google.maps.api.android.lib6.gmm6.c.g.a(Unknown Source)
                at com.google.maps.api.android.lib6.c.i.a(Unknown Source)
                at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
                at com.google.maps.api.android.lib6.c.ab.a(Unknown Source)
                at com.google.maps.api.android.lib6.c.aa.a(Unknown Source)
                at com.google.android.gms.maps.internal.x.onTransact(SourceFile:107)
                at android.os.Binder.transact(Binder.java:380)
                at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
                at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
                at com.google.android.gms.dynamic.a$4.b(Unknown Source)
                at com.google.android.gms.dynamic.a.a(Unknown Source)
                at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
                at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)
                at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)
                at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
                at android.app.Activity.setContentView(Activity.java:2241)
                at project.sharethefare.CurrentLocation.onCreate(CurrentLocation.java:24)
                at android.app.Activity.performCreate(Activity.java:6221)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
                at android.app.ActivityThread.access$900(ActivityThread.java:172)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5834)
                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:1388)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

This is the CurrentLocation class:

    package project.sharethefare;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;


import android.location.Location;
import android.view.View;

public class CurrentLocation extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    //________________________________________________________________________________________

    @Override
    protected void onCreate(Bundle savedInstanceState) {        //auto generated
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current_location);
        setUpMapIfNeeded();                                     //part of google maps api
        mMap.setMyLocationEnabled(true);                               //creates a new HomeScreen
    }


    @Override
    protected void onResume() {                             //Auto Generated
        super.onResume();
        setUpMapIfNeeded();                                 //set up map if not already created
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.curLocMap))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    //call the method to continuously check current location
    private void setUpMap() {
        mMap.setOnMyLocationChangeListener(myLocationChangeListener);
    }

    //called above. Used to constantly update the users position on the map
    private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            //create a new latitude and longitude point
            LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
            //make global variables on home screen == to current location
            HomeScreen.curLat = location.getLatitude();
            HomeScreen.curLong = location.getLongitude();
            HomeScreen.curLocSet = true;
            //animate the camera to zoom in on position when found
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
        }
    };

    public void backToHome(View view){ // called when button clicked. returns to homeScreen activity
        Intent intent = new Intent(CurrentLocation.this,HomeScreen.class);
        startActivity(intent);
    }

}

This is the corresponding xml file:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#000">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"

        android:layout_marginTop="60dp"
        android:layout_marginBottom="50dp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/curLocMap"
        tools:context="project.sharethefare.CurrentLocation"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="70dp"
        android:text="@string/set_Cur_Loc"
        android:textSize="20sp"
        android:onClick="backToHome"
        android:background="@drawable/button_design"
        android:textStyle="bold"/>

</LinearLayout>

And this is the manifest file:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="project.sharethefare" >

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

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <!--
 The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".HomeScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

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

        <activity
            android:name=".CurrentLocation"
            android:label="@string/title_activity_current_location" >
        </activity>
        <activity
            android:name=".Destination"
            android:label="@string/title_activity_maps" >
        </activity>
        <activity
            android:name=".Share"
            android:label="@string/title_activity_share" >
        </activity>
        <activity
            android:name=".SplashScreen"
            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.geo.API_KEY"
            android:value=""/>

    </application>

</manifest>
1
  • 1
    Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.maps.v2.API_KEY in the <application> element of AndroidManifest.xml Commented Mar 20, 2015 at 23:44

3 Answers 3

6
Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.maps.v2.API_KEY in the <application> element of AndroidManifest.xml

Based upon the error message, delete this from your manifest:

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyCgOCmQbNnBUd_6HeB5DmTfHXPaxmmJJtk"/>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks you very much this works, There was two keys i never noticed that.but now my map isn't showing up any ideas?
@McT: You probably want to switch to getMapAsync() instead of getMap(). They also just released a new version of the Play Services SDK, which I haven't tried yet. You'll want to double-check your API key value to make sure that it is correct. Beyond that, the only thing that I can suggest is to check my directory of map samples and see if you can get ones like Basic working, to confirm it is not an environment issue.
Okay no problem, I did have the application working fully but it just seems to messed up now, I'l definitely have a look at that. thanks for your help!
If you are using Android Studio and your project has this file "google_maps_api.xml", it should be under ".../Debug/Res/Values/", you can't have the meta-data in your AndroidManifest.xml file, or you will get this "API key can only be specifiend once" error
4

If you are using both Maps & Places Api in your application then you only need to specify geo api key

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="AIzaSyCgOCmQbNnBUd_6HeB5DmTfHXPaxmmJJtk"/>

Both apis will work properly.You can not use both meta data tags for google apis.

1 Comment

Thank you! I was working on this for hours and this was the issue.
1

In my case I have Google Map & Place API (PlaceLikelihoodBuffer).

Manifest file does not support two meta data for Google API key.

I have commented Google map API key for Map. Looks like below:

 <!-- <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBqXxRIxL4nK7BarmN6qQB48kkAbdp1Kbk" />\  -->
      <meta-data
            android:name="com.crashlytics.ApiKey"
            android:value="5b03ad6e904c61e5d9bfe6e499e749671d9eaea4" />

Fortunately Google Map & Place API (PlaceLikelihoodBuffer) both worked for me.

Strange but true.

Hope this will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.