将选择器自动关闭 [英] Place Picker automatically closing

查看:164
本文介绍了将选择器自动关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android系统制作一个简单的地方选择器程序和问题是,当我点击按钮的地方选择器打开,2后自动关闭 - !数秒请帮助

MainActivity.java

 包com.example.akshay.myapplication;进口android.content.Intent;进口android.support.v7.app.ActionBarActivity;
进口android.os.Bundle;进口android.view.View;
进口android.widget.Button;
进口android.widget.Toast;进口com.google.android.gms.common.GooglePlayServicesNotAvailableException;
进口com.google.android.gms.common.GooglePlayServicesRepairableException;
进口com.google.android.gms.location.places.Place;
进口com.google.android.gms.location.places.ui.PlacePicker;
进口com.google.android.gms.maps.model.LatLng;
进口com.google.android.gms.maps.model.LatLngBounds;公共类MainActivity扩展ActionBarActivity实现View.OnClickListener {    按钮B;
    私有静态最终诠释PLACE_PICKER_REQUEST = 1;
    私有静态最后的LatLngBounds BOUNDS_MOUNTAIN_VIEW =新的LatLngBounds(
            新的经纬度(37.398160,-122.180831),新的经纬度(37.430610,-121.972090));
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        B =(按钮)findViewById((R.id.bOpenMap));
        b.setOnClickListener(本);
    }    @覆盖
    公共无效的onClick(视图v){
            尝试{
                PlacePicker.IntentBuilder intentBuilder =
                        新PlacePicker.IntentBuilder();
                intentBuilder.setLatLngBounds(BOUNDS_MOUNTAIN_VIEW);
                意图意图= intentBuilder.build(getApplicationContext());
                startActivityForResult(意向,PLACE_PICKER_REQUEST);
            }赶上(GooglePlayServicesRepairableException E){
                e.printStackTrace();
            }赶上(GooglePlayServicesNotAvailableException E){
                e.printStackTrace();
            }
        }    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(要求code == PLACE_PICKER_REQUEST){
            如果(结果code == RESULT_OK){
                。地方= PlacePicker.getPlace(数据,本);
                字符串toastMsg =的String.format(地点:%S,place.getName());
                Toast.makeText(这一点,toastMsg,Toast.LENGTH_LONG).show();
            }
        }
    }
}

activity_main.xml中

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=MainActivity。>    <按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=打开地图
        机器人:ID =@ + ID / bOpenMap
        机器人:layout_alignParentTop =真
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentStart =真/>
< / RelativeLayout的>

AndroidManifest:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.akshay.myapplication>
    <使用许可权的android:NAME =com.example.akshay.myapplication.permission.MAPS_RECEIVE/>
    <使用许可权的android:NAME =com.google.android.providers.gsf.permission.READ_GSERVICES/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>    <! - 必须出示当前位置 - >
    <使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>
    <应用
        机器人:allowBackup =真
        机器人:图标=@的mipmap / ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>        < /活性GT;
        &所述;元数据
            机器人:名字=com.google.android.gms.version
            机器人:值=@整数/ GOOGLE_PLAY_SERVICES_VERSION/>        &所述;元数据
            机器人:名字=com.google.android.maps.v2.API_KEY
            机器人:值=************** API-KEY **********/>
    < /用途>< /清单>

LogCat中

  07-11 22:49:26.314 15108-15108 / com.example.akshay.myapplication E /受精卵:MountEmulatedStorage()
07-11 22:49:26.314 15108-15108 / com.example.akshay.myapplication E /合子:V2
07-11 22:49:26.314 15108-15108 / com.example.akshay.myapplication I / SELinux的:功能:selinux_compare_spd_ram,SPD-政策存在。 and_ver = SEPF_SM-N910G_5.0.1版本= 22
07-11 22:49:26.314 15108-15108 / com.example.akshay.myapplication I / SELinux的:功能:selinux_compare_spd_ram,优先[1],优先版本是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:变量seinfo:默认灵敏度:NULL,cateogry:NULL
07-11 22:49:26.314 15108-15108 / com.example.akshay.myapplication I /艺术:晚启用-Xcheck:JNI
07-11 22:49:26.314 15108-15108 / com.example.akshay.myapplication I / libpersona:KNOX_SDCARD检查此为10277
07-11 22:49:26.314 15108-15108 / com.example.akshay.myapplication I / libpersona:KNOX_SDCARD不是人物
07-11 22:49:26.354 15108-15108 / com.example.akshay.myapplication D / TimaKeyStoreProvider:TimaSignature不可用
07-11 22:49:26.354 15108-15108 / com.example.akshay.myapplication D / ActivityThread:添加TimaKeyStore提供商
07-11 22:49:26.444 15108-15108 / com.example.akshay.myapplication D / ResourcesManager:创造新的AssetManager并设置为/data/app/com.example.akshay.myapplication-2/base.apk
07-11 22:49:26.614 15108-15108 / com.example.akshay.myapplication D /活动:performCreate呼叫secproduct功能valuefalse
07-11 22:49:26.614 15108-15108 / com.example.akshay.myapplication D /活动:performCreate呼叫调试弹性valuetrue
07-11 22:49:26.664 15108-15193 / com.example.akshay.myapplication D / OpenGLRenderer:渲染脏区要求:真
07-11 22:49:26.704 15108-15193 / com.example.akshay.myapplication I /的Adreno:EGLInit:QTI生成:15年3月2日,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:初始化EGL,1.4版本
07-11 22:49:26.714 15108-15193 / com.example.akshay.myapplication I /的Adreno:GetNativeFor​​matFromQctPixelFormat:无效QCT格式(611)
07-11 22:49:26.714 15108-15193 / com.example.akshay.myapplication I /的Adreno:GetNativeFor​​matFromQctPixelFormat:无效QCT格式(611)
07-11 22:49:26.714 15108-15193 / com.example.akshay.myapplication I /的Adreno:GetNativeFor​​matFromQctPixelFormat:无效QCT格式(611)
07-11 22:49:26.714 15108-15193 / com.example.akshay.myapplication I /的Adreno:GetNativeFor​​matFromQctPixelFormat:无效QCT格式(611)
07-11 22:49:26.714 15108-15193 / com.example.akshay.myapplication I /的Adreno:GetNativeFor​​matFromQctPixelFormat:无效QCT格式(612)
07-11 22:49:26.714 15108-15193 / com.example.akshay.myapplication I /的Adreno:GetNativeFor​​matFromQctPixelFormat:无效QCT格式(612)
07-11 22:49:26.714 15108-15193 / com.example.akshay.myapplication I /的Adreno:GetNativeFor​​matFromQctPixelFormat:无效QCT格式(612)
07-11 22:49:26.714 15108-15193 / com.example.akshay.myapplication I /的Adreno:GetNativeFor​​matFromQctPixelFormat:无效QCT格式(612)
07-11 22:49:26.714 15108-15193 / com.example.akshay.myapplication D / OpenGLRenderer:获取最大纹理尺寸。 GL_MAX_TEXTURE_SIZE为16384
07-11 22:49:26.714 15108-15193 / com.example.akshay.myapplication D / OpenGLRenderer:启用调试模式0
07-11 22:49:26.804 15108-15108 / com.example.akshay.myapplication I /时间轴:时间轴:Activity_idle ID:android.os.BinderProxy@2a94005b时间: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:手柄0xaf917bb0上0xafb0b600(RippleDrawable)endAllStagingAnimators
07-11 22:49:32.774 15108-15108 / com.example.akshay.myapplication I /时间轴:时间轴:Activity_idle ID:android.os.BinderProxy@2a94005b时间:38359001
07-11 22:51:44.224 15108-15108 / com.example.akshay.myapplication D / ViewRootImpl:ViewPostImeInputStage ACTION_DOWN


解决方案

我跑你code和配置,只见​​那你看到相同的行为。

您的配置进行比较,以一个我工作后,我意识到,问题是,在你的Andr​​oidManifest.xml中,您需要更改此:

 的android:NAME =com.google.android.maps.v2.API_KEY

要这样:

 的android:NAME =com.google.android.geo.API_KEY

我回去,并在这两种配置测试多次,只是为了确保这是什么造成您的问题。使用第一配置中,PlacePicker立即每次关闭。

使用 com.google.android.geo.API_KEY ,它的每一次完美的作品。

这也是什么状态,你需要使用Places API的文档rel=\"nofollow\">。

与code测试,需要经过一次修改的结果,使其工作:

选择一个地点后:

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

解决方案

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:

After picking a Place:

这篇关于将选择器自动关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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