我是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>
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();
}
}
}
}
结束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
也许有些人有想法
答案 0 :(得分:0)
您必须在Androidmanifest Application标签中添加apiKey
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyD9MY-KYYF-lSXs_mOAZHY"/>
答案 1 :(得分:0)
确保您已启用 Places API ,不 Maps API:
这是一个常见的错误。在Google Cloud Library上找到它。 然后创建API密钥并将其添加到清单中。您可以按this guide限制访问您的应用。