【问题标题】:Place API closes immediately after openingPlace API 打开后立即关闭
【发布时间】:2018-05-02 17:58:15
【问题描述】:

我是 Android 的初学者 我正在尝试编写使用 Android Place API 的应用程序。 但是当我将手机连接到互联网时,这个地方 API 在打开后立即关闭,当我断开互联网并打开 PlacePicker 时它没有关闭,但是当我打开 PlacePicker 并重新连接到互联网并输入一些搜索时:可以不加载搜索结果 我读到这个: Android Place Picker closes immediately after launch 我检查了所有的建议,但一直都是一样的 Logcat 没有错误 我的英语不好,但我希望你能理解我:-)

下面添加代码 清单:

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

分级

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

 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();
                }
            }
        }
    }

结束日志:

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

也许你们中的一些人有一个想法

【问题讨论】:

  • 你错过了&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;的权限
  • 我加了这个不行

标签: java android google-places-api


【解决方案1】:

确保您已启用 Places API,并且 Maps API:

这是一个常见的错误。在Google Cloud Library 上找到它。 然后创建 API 密钥并将其添加到清单中。您可以关注this guide 以限制其访问您的应用。

【讨论】:

    【解决方案2】:

    您必须像在 Androidmanifest Application 标签中一样添加 apiKey

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

    【讨论】:

    • 不知道卸载/重新安装应用清理项目
    • 我试试这个但没有帮助
    猜你喜欢
    • 2017-08-20
    • 2017-11-21
    • 2014-11-23
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2014-06-14
    相关资源
    最近更新 更多