发射后立即关闭拾取器

时间:2018-02-11 16:21:50

标签: android google-places-api

我想显示用户可以选择位置的地方选择器。但是地点选择器活动在启动后会自动关闭。我多次检查API密钥也在stackoverflow中搜索此主题,但没有任何评论对我有用。 这是我的代码。请帮忙

这是Magnifest文件

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.wifi" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

Build.gradle(app module)

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.4.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.4.0'
compile 'com.google.android.gms:play-services-maps:11.8.0'
compile 'com.google.android.gms:play-services-places:11.8.0'
compile 'com.google.android.gms:play-services-location:11.8.0'
testCompile 'junit:junit:4.12'}

apply plugin: 'com.google.gms.google-services'

这是一项活动

int PLACE_PICKER_REQUEST = 1;
TextView tvPlace;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pick_map);

    tvPlace = (TextView) findViewById(R.id.tvPlace);

    Button btnLocate = (Button) findViewById(R.id.btnPickhome);
    btnLocate.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
            try {
                Intent intent = builder.build(PickMapActivity.this);
                startActivityForResult(intent, PLACE_PICKER_REQUEST);
            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == RESULT_OK) {

            Place place = PlacePicker.getPlace(this, data);
            tvPlace.setText(place.getAddress());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试在真实手机中部署您的应用程序而不是模拟器....发布您的日志?Places API在日志中抛出异常