简体   繁体   中英

Place API closes immediately after opening

I'm a beginner in Android I'm trying to write apps that use Android Place API. but when I have a phone connected to the internet this place API closes immediately after opening, when I disconnect the internet and open PlacePicker it didn't close but when I have open PlacePicker and re-connecting to the internet and typing some search: Can not load search results I read this: Android Place Picker closes immediately after launch I checked all the suggestions but all the time it's the same Logcat has no errors My English is not well but I have hope you understand me :-)

Below adds the code Manifests:

    <?xml version="1.0" encoding="utf-8"?>

<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: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/key_api">
    </meta-data>

    <activity android:name=".MainActivity">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.dawid.recruitmenttask"
        minSdkVersion 18
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.android.gms:play-services-maps:11.6.0'
    implementation 'com.google.android.gms:play-services-location:11.6.0'
    implementation 'com.google.android.gms:play-services-places:11.6.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

java class:

 package com.example.dawid.recruitmenttask;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    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;

    public class MainActivity extends AppCompatActivity {

        int     PLACE_PICKER_REQUEST = 1;
        private Button serchBut;

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

            serchBut = (Button) findViewById(R.id.go_to_search);

            serchBut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    openSerch();
                }
            });
        }

        public void openSerch () {
            PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

            try {
                startActivityForResult(builder.build(MainActivity.this), 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(MainActivity.this, data);
                    String toastMsg = String.format("Place: %s", place.getName());
                    Toast.makeText(this, toastMsg, Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

end logcat:

11-19 12:03:55.126 8164-8164/com.example.dawid.recruitmenttask I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
11-19 12:03:55.144 8164-8177/com.example.dawid.recruitmenttask D/InputTransport: Input channel constructed: fd=79
11-19 12:03:55.147 8164-8164/com.example.dawid.recruitmenttask V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@5292d3c nm : com.example.dawid.recruitmenttask ic=null
11-19 12:04:26.251 8164-8164/com.example.dawid.recruitmenttask D/ViewRootImpl@f1a1c3f[MainActivity]: ViewPostImeInputStage processPointer 0
11-19 12:04:26.337 8164-8164/com.example.dawid.recruitmenttask D/ViewRootImpl@f1a1c3f[MainActivity]: ViewPostImeInputStage processPointer 1
11-19 12:04:26.410 8164-8164/com.example.dawid.recruitmenttask D/ViewRootImpl@f1a1c3f[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
11-19 12:04:27.084 8164-8164/com.example.dawid.recruitmenttask D/InputTransport: Input channel destroyed: fd=79
11-19 12:04:27.673 8164-8164/com.example.dawid.recruitmenttask D/ViewRootImpl@f1a1c3f[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
11-19 12:04:27.673 8164-8164/com.example.dawid.recruitmenttask D/ViewRootImpl@f1a1c3f[MainActivity]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true -1518911488}
11-19 12:04:27.674 8164-8164/com.example.dawid.recruitmenttask V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@cb07d7d nm : com.example.dawid.recruitmenttask ic=null
11-19 12:04:27.674 8164-8164/com.example.dawid.recruitmenttask I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
11-19 12:04:27.686 8164-8164/com.example.dawid.recruitmenttask D/InputTransport: Input channel constructed: fd=85

maybe some of you have an idea

You have to add the apiKey like in the Androidmanifest Application tag

<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyD9MY-KYYF-lSXs_mOAZHY"/>

Make sure that you have enabled the Places API , and NOT the Maps API:

Places API

Which is a common mistake. Find it on the Google Cloud Library . Then create and add the API Key to your manifest. You can follow this guide for restricting it the access to your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM