Android Place Picker在发布后立即关闭

Sam*_*han 52 android google-places-api

我正在开发一个Android应用程序作为项目的一部分,并使用Google place API根据位置显示感兴趣的地方.我正在使用PlacePicker Inentbuilder来实现这一目标.

但是,当应用程序运行时,位置选择器启动然后立即关闭(大约1-2秒).

我已经实现了以下建议(我从其他答案得到):

我已经为Android应用程序生成了公共API密钥,并将其包含在应用程序清单中的元数据标记中.

我在开发者控制台上启用了"Google Places API for android"API.

我在build.gradle中包含了最新的play服务版本的依赖项.

我已将我的代码和logcat包含在下面.如果我需要包含其他任何内容,请告诉我.

的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>
Run Code Online (Sandbox Code Playgroud)

Build.gradle(app模块 - 这是唯一的模块)

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'
}
Run Code Online (Sandbox Code Playgroud)

PlacesSample - 使用Google地方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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Sam*_*han 65

Francois Wouts的解决方案帮助解决了这个问题.谢谢Francois ......

我使用关键字"Places"搜索日志,发现Places API确实引发了异常.它期望在Manifest.xml中com.google.android.geo.API_KEY<application>标签内.

我改变了对com.google.android.geo.API_KEY<activity>标签,而不是一个在<application>标签.

现在更改为com.google.android.geo.API_KEY并从<activity>标记中删除相同的行,并使其正常工作.感觉就像一个白痴因为我自己没有这样做.

元数据标记应该读取android:name="com.google.android.geo.API_KEY"
它应该<application>在Manifest中的标记内.

  • 我在<application>下有它,但仍然没有用. (5认同)
  • 这让我,我在<manifest>下了...谢谢! (3认同)
  • 不工作我在应用程序标签下有它 (2认同)

Fra*_*uts 30

在此输入图像描述您是否在开发者控制台中仔细检查了您的API密钥是否与您的应用程序(应用程序证书的软件包名称和SHA-1指纹)相关联?

您可以在注册和API密钥中找到说明.确保为调试和发布证书设置它.

我希望有所帮助!


Ani*_*ban 19

我遇到了同样的问题.请务必在开发者控制台中启用Google Places API for Android,而不仅仅是Places API."Places API for Android"不会出现在API和Auth/API下,因为它还不是一种流行的API.您必须使用API​​搜索框进行搜索.

  • 在我的情况下,这是问题,我必须在Developer API控制台上启用Google Places API (3认同)
  • 这是一个救生员. (2认同)

Har*_*esh 5

在清单文件中,com.google.android.geo.api_keycom.google.android.maps.v2.API_KEY不应相同.

转到https://console.developers.google.com/project

登录并按照以下步骤获取placepicker位置的密钥.

创建或选择现有>使用google apis> 适用于Android的Google Places API >在左侧菜单中启用>凭据>添加凭据> api密钥> android密钥>创建>复制密钥.

将密钥粘贴到清单" com.google.android.geo.api_key "

注意:google places api每天限制为1000个请求.你必须付钱后.最好避免使用PlacePicker.

  • 我认为Google目前使用单个API密钥来处理所有API. (2认同)