Android Place Picker在启动后立即关闭 [英] Android Place Picker closes immediately after launch

查看:43
本文介绍了Android Place Picker在启动后立即关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序作为项目的一部分,并且正在使用Google places API根据位置显示感兴趣的地方.我正在使用PlacePicker Inentbuilder来完成此操作.

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.

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

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):

我已经为Android应用程序生成了公共API密钥,并将其包含在应用程序清单的meta-data标签中.

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

我已在开发人员控制台上启用了适用于Android的Google Places API" API.

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

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

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

我在下面包含了我的代码和logcat.让我知道是否需要添加其他内容.

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

Manifest.xml:

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(应用程序模块-这是唯一的模块)

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-使用Google Places API的活动:

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

推荐答案

Francois Wouts的解决方案有助于回答这一问题.谢谢弗朗索瓦...

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

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

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.

我已将<activity>标记中的内容更改为com.google.android.geo.API_KEY,而不是<application>标记中的内容.

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

现在更改为com.google.android.geo.API_KEY,并从<activity>标记中删除了相同的行,并使其起作用.感觉像个白痴,因为我自己没有解决这个问题.

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..

元数据标签应显示为android:name="com.google.android.geo.API_KEY"
它应该在清单的<application>标记内.

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

这篇关于Android Place Picker在启动后立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆