Android Place Picker closes immediately after launch
Asked Answered
B

15

53

I am developing an android application as part of a project, and am using Google places API to display places of interest based on location. I am using the PlacePicker Inentbuilder to accomplish this.

However, when the app is run, the place picker launches and then closes immediately (about 1-2 seconds).

I have already implemented the below suggestions (that I got from other answers):

I have generated the public API key for android applications, and am including this in the meta-data tag in the app manifest.

I have enabled the "Google Places API for android" API on the developers console.

I have included the latest play services version in dependencies in build.gradle.

I have included my code and the logcat below. Do let me know if I need to include anything else.

Manifest.xml:

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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.gms.version"
        android:value="@integer/google_play_services_version" />

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

    <activity
        android:name=".LoginActivity"
        android:label="@string/title_activity_login" >
    </activity>
    <activity
        android:name=".PlacesSample"
        android:label="@string/title_activity_places_sample" >
        <meta-data
            android:name="com.google.android.geo.api_key"
            android:value="@string/google_api_key" />
    </activity>
</application>

</manifest>

Build.gradle (app module - This is the only module)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.sampath.project.project_v2"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    //compile fileTree(include: ['*.jar'], dir: 'libs')
    //compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:cardview-v7:22.1.1'
    compile 'com.android.support:recyclerview-v7:22.1.1'
    compile 'com.google.android.gms:play-services:7.3.0'
}

PlacesSample - Activity that is using google places API:

package com.sampath.project.project_v2;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;


public class PlacesSample extends AppCompatActivity {
    TextView getLocation;
    int PLACE_PICKER_REQUEST = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places_sample);
        getLocation = (TextView)findViewById(R.id.getLocTV);
        getLocation.setClickable(true);
        getLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
                Intent intent;
                try {
                    intent = builder.build(getApplicationContext());
                    startActivityForResult(intent, PLACE_PICKER_REQUEST);
                    System.out.println("start activity for result");
                } catch (GooglePlayServicesRepairableException e) {
                    e.printStackTrace();
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_places_sample, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        System.out.println("onActivityResult");
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {
                Place place = PlacePicker.getPlace(data, this);
                String toastMsg = String.format("Place: %s", place.getName());
                Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
            }
        }
    }
}

Logcat:

05-05 23:38:30.593  21408-21408/com.sampath.project.project_v2 I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@17e945c6 time:628772943
05-05 23:38:30.598  21408-21408/com.sampath.project.project_v2 I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@17e945c6 time:628772948
05-05 23:38:31.517  21408-21408/com.sampath.project.project_v2 I/Timeline﹕ Timeline: Activity_launch_request id:com.sampath.project.project_v2 time:628773867
05-05 23:38:31.527  21408-21408/com.sampath.project.project_v2 W/ResourceType﹕ For resource 0x01030224, entry index(548) is beyond type entryCount(9)
05-05 23:38:31.527  21408-21408/com.sampath.project.project_v2 W/ResourceType﹕ For resource 0x01030224, entry index(548) is beyond type entryCount(9)
05-05 23:38:31.636  21408-21408/com.sampath.project.project_v2 I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2daadb0a time:628773986
05-05 23:38:33.869  21408-21408/com.sampath.project.project_v2 I/System.out﹕ start activity for result
05-05 23:38:34.227  21408-21408/com.sampath.project.project_v2 I/System.out﹕ onActivityResult
05-05 23:38:34.235  21408-21408/com.sampath.project.project_v2 I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2daadb0a time:628776586
Brachycephalic answered 6/5, 2015 at 3:54 Comment(3)
can you provide your whole code? Because I've also the same error and unable to solve from your answer.Baldheaded
have you got any solution @sampath janardhan?Meras
The Google Play Services version of the Places SDK for Android (in Google Play Services 16.0.0) is deprecated as of January 29, 2019, and will be turned off on July 29, 2019. A new version of the Places SDK for Android is now available. We recommend updating to the new version as soon as possible. For details, see the migration guide.Phylis
B
65

Francois Wouts' solutions helped answer this. Thank you Francois...

I searched the logs with keyword 'Places' and found that Places API was indeed throwing an exception. It expected the com.google.android.geo.API_KEY within the <application> tags in the Manifest.xml.

I had changed to com.google.android.geo.API_KEY in the <activity> tag and not the one in the <application> tag.

Now changed to com.google.android.geo.API_KEY and removed the same lines from <activity> tag, and got it working. Feel like an idiot for not working this out by myself..

The meta-data tag should read android:name="com.google.android.geo.API_KEY"
It should be within the <application> tag in the Manifest.

Brachycephalic answered 7/5, 2015 at 1:22 Comment(2)
I have it under <application> but still this is not working.Fargone
As we generating API key from google's developer console, we should enable the API such as GeoCode, PlaceSearch, etc. My experience: I was tried to use placeautocomplete without enable the API so it closes immediately when tried open it. Then I enabled and it's working fine.Fabulist
S
31

enter image description hereHave you double checked that your API key is associated with your application (package name and SHA-1 fingerprint of your app's certificate) in the Developer Console?

You can find instructions at Signup and API Keys. Make sure to set it up for both your debug and your release certificates.

I hope that helps!

Schoolhouse answered 6/5, 2015 at 4:16 Comment(5)
I did double check to ensure that the API key association is correct with my apps. I now added release certificate, but it didn't make any difference.Brachycephalic
I also see in your manifest that you used com.google.android.geo.api_key. Can you try com.google.android.geo.API_KEY instead? Source: developers.google.com/places/android/start#api-keySchoolhouse
I tried changing to com.google.android.geo.API_KEY. Have the same results. (Also have removed com.sampath.project.project_v2.API_KEY with the same results). NOTE: I tried running the example code put on github by google. With the same results. So I'm guessing there is some problem with the API itself.. I would be grateful if anybody could run the sample code on your system and verify that it ain't working. Link to code samplesBrachycephalic
The logs that you gave above are filtered by your app package. However the Place Picker runs outside of your app (in Google Play Services), so error messages would not be in here. Could you use "No filters" instead of "Show only selected application"? I recommend looking for "E/Place" in your logs.Schoolhouse
when debug api key for keyrestriction non working fine.when i generate signes apk and added key restriction for android app with package name and sha1 showing exception invalid api key Unexpected response code 403Pelt
T
19

I was having the same issue. Make sure you enable Google Places API for Android and not just Places API in the Developer Console. "Places API for Android" will not show up under APIs & Auth/APIs because it isn’t a popular API (yet). You will have to search for it using the API search box.

Tang answered 25/5, 2017 at 9:45 Comment(3)
In my case this was the issue, I have to enable Google Places API on Developer API ConsoleParticia
Thats a life saver.Daffi
Please help me to find Google Places API for Android.Binni
P
4

In manifest file com.google.android.geo.api_key and com.google.android.maps.v2.API_KEY shouldnot be same.

go to https://console.developers.google.com/project

login and follow these steps to get key for placepicker places.

create or choose existing > use google apis > Google Places API for Android > enable > credentials in left menu > add credentials > api key > android key > create > copy key.

paste your key at manifest "com.google.android.geo.api_key"

Note : there is a limit of 1000 requests per day for google places api. after you have to pay money. Better to avoid PlacePicker.

Purview answered 9/12, 2015 at 9:56 Comment(1)
I think Google as per now use a single API key for all their APIs.Needlewoman
T
2

In my case there was conflict of APIs. I have added google-service.json file which also had API key and I generated new key for Maps. So place picker was closing immediately. Solution :

  1. Use single API key which is in google-service.json file

  2. Enable "Places SDK for Android" feature for your Google Cloud Platform project (with same name as in Firebase project , don't use different project. You will find same name project as you are using in Firebase)

  3. Put SHA1 Application Restriction for android app (Recommended)
  4. Check if you have placed following code in Application tag only

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="API_KEY" />
    
Taxdeductible answered 6/8, 2018 at 18:43 Comment(2)
Should step 4 be in your Manifest file? or should it not in the Manifest file?Peregrine
Yes. Its should be.Taxdeductible
C
1

Add compile 'com.google.android.gms:play-services-location:8.1.0' in gradle

Currin answered 29/3, 2016 at 14:22 Comment(0)
C
1

This issue made me crazy as well, there are so many issues regarding exactly this problem, but no one was helpful for me. At the end, I figured out that there are different Google Places APIs especially for Android. Consequently, the API-Key that I was using was simply for the non-Android version.

Generate your key using this link https://developers.google.com/places/android-api/signup

Critchfield answered 8/10, 2017 at 18:45 Comment(0)
T
1

May be you forgot to add your machine SHA key to the Google Maps Console. Faced same issue, Added my SHA Key to the console, it started working fine.

Twinge answered 27/4, 2018 at 4:29 Comment(0)
K
1

I filling out the Consent screen in APIs & Auth of the console and its working fine. Check the screen shot. enter image description here

Kowalewski answered 18/5, 2018 at 6:51 Comment(0)
A
1

It was strange that it was a credentials problem for me. It works even without a warning in debugging mode but when i use released version of my app it just doesnt work (i have registered my release and debug sha1 keys) So i removed all restrictions and now its working.

Adapa answered 5/6, 2018 at 15:35 Comment(0)
F
0

I'll just drop this here, maybe it helps someone in the future.

For me, using the wrong Api key caused the crash, even though it worked fine in the past.

Places SDK for Android - Credentials

I had to use the second key (the one that is auto-created by Google services for Android) instead of the first one which i created.

Foxtrot answered 22/12, 2018 at 18:34 Comment(0)
B
0

In my case when I changed current API key with the one which is in google-service.json, it resolved the problem.

In google-services.json:

 "api_key": [
        {
          "current_key": "PASTE THIS API KEY TO THE MANIFEST"
        }
      ]
Bessel answered 12/8, 2019 at 13:50 Comment(0)
F
0

The PlacePicker is now officially deprecated.

I'm trying to complete the Udacity Advanced Android App Development course and running into the same issue. https://developers.google.com/places/android-sdk/client-migration#place-picker-deprecation says that

The Place Picker was deprecated on January 29, 2019. It was turned off on July 29, 2019, and is no longer available. Continued use will result in an error message. The new SDK does not support the Place Picker.

Cross-posting to https://github.com/googlemaps/android-places-demos/issues/2

Fajardo answered 29/12, 2019 at 17:9 Comment(0)
H
0

The PlacePicker is now officially deprecated.

Try this library. Light weight and similar to placepicker with some additional features.

https://github.com/BilalSiddiqui/AddressPicker

Honesty answered 3/2, 2020 at 9:28 Comment(0)
R
0

In my case I had not provided google with my billing account details so the api was was disabled even after enabling it.

If this is also the case for you, add the billing account in the google cloud console and finally you will be able to do it.

Rese answered 21/10, 2021 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.