You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Place Picker启动后自动关闭问题求助,已排查API Key及Stack Overflow

Fixing Place Picker Auto-Closing Issue

Hey there! Let's figure out why your Place Picker is shutting down right after launch. I've tackled this exact problem a bunch of times, so let's go through the most common fixes step by step:

1. Double-Check Google Places API & Billing Setup

This is the #1 culprit most people miss:

  • Head to your Google Cloud Console, find your project, and search for Places API. Make sure its status is set to "Enabled" (it's easy to forget to turn this on!).
  • Verify that you have a valid billing account linked to your project. Even if you're within free usage limits, Google requires a billing account to use Places API—no exceptions.

2. Validate Manifest Configuration

Make sure your AndroidManifest.xml has all the required bits:

  • Add the necessary permissions (place these outside the <application> tag):
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
  • Inside the <application> tag, confirm your API key is using the correct meta-data name (this is different from the old Maps API key):
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="YOUR_ACTUAL_API_KEY" />
    

3. Check Build.gradle Dependencies & Plugin Placement

  • Ensure you're using a compatible version of the Places library. If you're sticking with the legacy Place Picker, use a version like 16.0.0 (newer versions have deprecated Place Picker in favor of Autocomplete):
    dependencies {
        implementation 'com.google.android.gms:play-services-places:16.0.0'
        // Make sure other play-services dependencies match this version to avoid conflicts
    }
    
  • Confirm the google-services plugin is at the very bottom of your app-level build.gradle file—this ensures it processes all dependencies correctly.

4. Fix Activity Code & Permissions Handling

  • First, on Android 6.0+ (API 23+), you need to request location permissions dynamically before launching Place Picker. If you skip this, the system will silently kill the activity. Here's how to handle it:
    private static final int PERMISSION_REQUEST_LOCATION = 100;
    private static final int PLACE_PICKER_REQUEST = 1;
    
    // Call this when you want to launch Place Picker
    private void launchPlacePicker() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSION_REQUEST_LOCATION);
        } else {
            startPicker();
        }
    }
    
    private void startPicker() {
        PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
        try {
            startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
        } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
            // Show a toast to the user here so you know if Play Services is the issue
        }
    }
    
    // Handle permission request result
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQUEST_LOCATION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startPicker();
            } else {
                // User denied permission—show a message explaining why you need it
            }
        }
    }
    
  • Always catch those two exceptions when launching Place Picker—they'll tell you if Google Play Services is outdated or missing.

5. Verify Device's Google Play Services

Make sure the device you're testing on has the latest version of Google Play Services installed. Outdated versions can cause compatibility issues with Place Picker. You can check this in Settings > Apps > Google Play Services and tap "Update" if available.

6. Recheck API Key Restrictions

Even if you think your API key is correct, double-check its restrictions:

  • In Google Cloud Console, go to your API key's settings. If you've restricted it to Android apps, confirm the package name and SHA-1 fingerprint match exactly what's in your project. A tiny typo here will block the API call.

If none of these fixes work, grab the error logs from Logcat (look for lines tagged with AndroidRuntime or GooglePlaces). The crash log will give you the exact reason the activity is closing—this is the fastest way to pinpoint the issue!

内容的提问来源于stack exchange,提问作者nksbakery

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:40:58