1

I'm making a simple place picker program in android and the problem is when I click on button the place picker opens and automatically closes after 2 - 3 seconds... please help!

MainActivity.java

package com.example.akshay.myapplication;

import android.content.Intent;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
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;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    Button b;
    private static final int PLACE_PICKER_REQUEST = 1;
    private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
            new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = (Button) findViewById((R.id.bOpenMap));
        b.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
            try {
                PlacePicker.IntentBuilder intentBuilder =
                        new PlacePicker.IntentBuilder();
                intentBuilder.setLatLngBounds(BOUNDS_MOUNTAIN_VIEW);
                Intent intent = intentBuilder.build(getApplicationContext());
                startActivityForResult(intent, PLACE_PICKER_REQUEST);
            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }
        }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        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();
            }
        }
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Maps"
        android:id="@+id/bOpenMap"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.akshay.myapplication" >
    <uses-permission android:name="com.example.akshay.myapplication.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <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.maps.v2.API_KEY"
            android:value="**************API-KEY**********"/>
    </application>

</manifest>

LogCat

07-11 22:49:26.314  15108-15108/com.example.akshay.myapplication E/Zygote﹕ MountEmulatedStorage()
07-11 22:49:26.314  15108-15108/com.example.akshay.myapplication E/Zygote﹕ v2
07-11 22:49:26.314  15108-15108/com.example.akshay.myapplication I/SELinux﹕ Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-N910G_5.0.1 ver=22
07-11 22:49:26.314  15108-15108/com.example.akshay.myapplication I/SELinux﹕ Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SM-N910G_5.0.1_0022
07-11 22:49:26.314  15108-15108/com.example.akshay.myapplication E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
07-11 22:49:26.314  15108-15108/com.example.akshay.myapplication I/art﹕ Late-enabling -Xcheck:jni
07-11 22:49:26.314  15108-15108/com.example.akshay.myapplication I/libpersona﹕ KNOX_SDCARD checking this for 10277
07-11 22:49:26.314  15108-15108/com.example.akshay.myapplication I/libpersona﹕ KNOX_SDCARD not a persona
07-11 22:49:26.354  15108-15108/com.example.akshay.myapplication D/TimaKeyStoreProvider﹕ TimaSignature is unavailable
07-11 22:49:26.354  15108-15108/com.example.akshay.myapplication D/ActivityThread﹕ Added TimaKeyStore provider
07-11 22:49:26.444  15108-15108/com.example.akshay.myapplication D/ResourcesManager﹕ creating new AssetManager and set to /data/app/com.example.akshay.myapplication-2/base.apk
07-11 22:49:26.614  15108-15108/com.example.akshay.myapplication D/Activity﹕ performCreate Call secproduct feature valuefalse
07-11 22:49:26.614  15108-15108/com.example.akshay.myapplication D/Activity﹕ performCreate Call debug elastic valuetrue
07-11 22:49:26.664  15108-15193/com.example.akshay.myapplication D/OpenGLRenderer﹕ Render dirty regions requested: true
07-11 22:49:26.704  15108-15193/com.example.akshay.myapplication I/Adreno﹕ EGLInit: QTI Build: 03/02/15, 210b328, I0829b9e471, LA.BF.2.1.05.00.00.203.165
07-11 22:49:26.704  15108-15193/com.example.akshay.myapplication I/OpenGLRenderer﹕ Initialized EGL, version 1.4
07-11 22:49:26.714  15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-11 22:49:26.714  15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-11 22:49:26.714  15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-11 22:49:26.714  15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-11 22:49:26.714  15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-11 22:49:26.714  15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-11 22:49:26.714  15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-11 22:49:26.714  15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-11 22:49:26.714  15108-15193/com.example.akshay.myapplication D/OpenGLRenderer﹕ Get maximum texture size. GL_MAX_TEXTURE_SIZE is 16384
07-11 22:49:26.714  15108-15193/com.example.akshay.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0
07-11 22:49:26.804  15108-15108/com.example.akshay.myapplication I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2a94005b time:38353037
07-11 22:49:31.494  15108-15108/com.example.akshay.myapplication D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
07-11 22:49:31.694  15108-15193/com.example.akshay.myapplication D/OpenGLRenderer﹕ endAllStagingAnimators on 0xafb0b600 (RippleDrawable) with handle 0xaf917bb0
07-11 22:49:32.774  15108-15108/com.example.akshay.myapplication I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2a94005b time:38359001
07-11 22:51:44.224  15108-15108/com.example.akshay.myapplication D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Akshay
  • 35
  • 2
  • 10
  • Similar problem [reported here](http://stackoverflow.com/questions/31243930/google-placepicker-closes-immediately-after-launch-with-resultcode-2), although you're not seeing the logcat messages is he. – Bob Snyder Jul 11 '15 at 19:26

1 Answers1

0

I ran your code and configuration, and saw the same behavior that you were seeing.

After comparing your configuration to one that I had working, I realized that the issue is that in your AndroidManifest.xml, you need to change this:

android:name="com.google.android.maps.v2.API_KEY"

To this:

android:name="com.google.android.geo.API_KEY"

I went back and tested multiple times with both configurations just to make sure that this was what was causing your issue. Using the first configuration, the PlacePicker closes immediately every time.

Using com.google.android.geo.API_KEY, it works perfectly every time.

This is also what the documentation states you need to use for the Places API.

Result of testing with your code, and the one modification needed to make it work:

enter image description here

After picking a Place:

enter image description here

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Thank You So Much Buddy.. One More Question.. android:name="com.google.android.geo.API_KEY" .. It will work if i want to display v2 maps. not the place picker? – Akshay Jul 12 '15 at 07:11
  • @Akshay yes, it will work for maps API v2 as well, I also tested that! – Daniel Nugent Jul 12 '15 at 07:13