问题描述
我无法从我的片段中启动Google Place Picker。
遵循所有步骤和论坛的根源,但无法解决我的问题。
链接
已经检查以下内容没有任何成功
我创建了Android版Google Places API,并添加到了清单中。
我甚至尝试过构建版本。
我收到的错误信息是:
03-18 12:02:32.524 1679-1900 /? E / GCoreFlp:Google
内的位置请求播放服务必须包含一个标签以帮助进行调试。使用LocationRequestInternal.create来包装您的LocationRequest,并将其传递给requestLocationUpdates。
03-18 12:02:32.527 29916-29916 /? E / PlacePicker:由于错误
需要帮助
我的清单文件
<?xml version =1.0encoding =utf-8? >
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =com.tyagiabhinav.test>
<使用权限android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.INTERNET/>
< meta-data
android:name =com.google.android.geo.API_KEY
android:value =KEY-REMOVED-ON-STACKOVERFLOW/ >
< meta-data
android:name =com.google.android.gms.version
android:value =@ integer / google_play_services_version/>
< application
android:allowBackup =true
android:icon =@ mipmap / ic_launcher
android:label =@ string / app_name
android:supportsRtl =true
android:theme =@ style / AppTheme>
< activity android:name =。MainActivity>
< intent-filter>
< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< / application>
< / manifest>
我的Gradle文件
apply plugin:'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion23.0.2
defaultConfig {
applicationIdcom.tyagiabhinav.test
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName1.0
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
}
依赖关系{
编译fileTree(dir:'libs',include:['* .jar'])
testCompile'junit:junit:4.12'
编译'com.android.support:appcompat-v7:23.1.1'
编译'com.google.android.gms:play-services-location:8.4.0'
}
我的片段代码
package com.tyagiabhinav.test;
导入android.Manifest;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.os.Bundle;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.app.Fragment;
导入android.support.v4.content.ContextCompat;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.View;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.location.places.ui.PlacePicker;
/ **
*由abhinavtyagi于18/03/16创建。
* /
public class MainFragment extends Fragment implements ConnectionCallbacks,OnConnectionFailedListener {
$ b $ private查看rootView;
私有GoogleApiClient mGoogleApiClient;
private boolean isLocationServiceConnected = false;
private static final String LOG_TAG = MainFragment.class.getSimpleName();
private static final int PLACE_PICKER_REQUEST = 7;
@Override
public view onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.main_fragment,container,false);
if(mGoogleApiClient == null){
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
。 addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
}
按钮btn =(按钮)rootView.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Log.d(LOG_TAG,Clicked);
if(ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED&&& ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission .ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
Log.d(LOG_TAG,询问权限);
//是否应该显示解释?
if(ActivityCompat .shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION)){
//异步显示给用户expanation * - 不要阻止
// // th是线程等待用户的回应!在用户
//看到解释后,再次尝试请求权限。
requestPermissions(new String [] {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},PLACE_PICKER_REQUEST);
} else {
//不需要解释,我们可以申请许可。
requestPermissions(new String [] {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},PLACE_PICKER_REQUEST);
}
} else {
Log.d(LOG_TAG,Permission Available);
placePicker();
}
}
});
返回rootView;
private void placePicker(){
if(isLocationServiceConnected){
Log.d(LOG_TAG,Connected to location service);
PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
尝试{
Intent intent = intentBuilder.build(getActivity());
startActivityForResult(intent,PLACE_PICKER_REQUEST);
} catch(GooglePlayServicesRepairedException e){
e.printStackTrace();
} catch(GooglePlayServicesNotAvailableException e){
e.printStackTrace();
}
} else {
Log.d(LOG_TAG,未连接到位置服务);
Toast.makeText(getActivity(),未连接到位置服务,Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions [],int [] grantResults){
Log.d LOG_TAG,onRequestPermissionsResult);
switch(requestCode){
case PLACE_PICKER_REQUEST:{
//如果请求被取消,结果数组是空的。 (grant_results.length> 0&&& amp; grantResults [0] == PackageManager.PERMISSION_GRANTED){
Log.d(LOG_TAG,Permission Granted);
//许可被授予,耶!做你需要做的
//联系人相关的任务。
placePicker();
} else {
Log.d(LOG_TAG,Permission Denied);
//权限被拒绝,嘘!禁用依赖此权限的
//功能。
Toast.makeText(getActivity(),访问Place Picker需要位置权限!,Toast.LENGTH_LONG).show();
}
return;
}
//其他'case'行检查其他
//权限此应用可能请求
}
}
@Override
public void onStart(){
super.onStart();
if(mGoogleApiClient!= null)
mGoogleApiClient.connect();
$ b $ @覆盖
public void onStop(){
if(mGoogleApiClient!= null&& mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}
super.onStop();
$ b @Override
public void onConnectionFailed(ConnectionResult connectionResult){
Log.d(LOG_TAG,Location Connection Failed);
isLocationServiceConnected = false;
}
@Override
public void onConnected(Bundle bundle){
Log.d(LOG_TAG,Location Service Connected);
isLocationServiceConnected = true;
}
@Override
public void onConnectionSuspended(int i){
Log.d(LOG_TAG,Location Service Suspended);
isLocationServiceConnected = false;
}
}
错误地放置了元数据标签
更正了清单文件:P
<?xml version =1.0encoding =utf-8?>
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =com.tyagiabhinav.test>
<使用权限android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.INTERNET/>
< application
android:allowBackup =true
android:icon =@ mipmap / ic_launcher
android:label =@ string / app_name
android:supportsRtl =true
android:theme =@ style / AppTheme>
< meta-data
android:name =com.google.android.geo.API_KEY
android:value =KEY-REMOVED-ON-STACKOVERFLOW/ >
< meta-data
android:name =com.google.android.gms.version
android:value =@ integer / google_play_services_version/>
< activity android:name =。MainActivity>
< intent-filter>
< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< / application>
< / manifest>
I am not able to launch Google Place Picker from my fragment.Followed all the steps and forums for the root cause but not able to resolve my issue.
Links Google Place Picker
Already checked following without any success
Android Place Picker closes immediately after launch
Place Picker Automatically close after launch
I have created Google Places API for Android key and added to manifest
I have even tried a release build.
Only error message I receive is
03-18 12:02:32.524 1679-1900/? E/GCoreFlp: Location requests inside Google
Play services must contain a tag to aid in debugging. Use LocationRequestInternal.create to wrap your LocationRequest, and pass it to requestLocationUpdates.
03-18 12:02:32.527 29916-29916/? E/PlacePicker: Place Picker closing due to ERROR
Need Help
My Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tyagiabhinav.test">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="KEY-REMOVED-ON-STACKOVERFLOW" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My Gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.tyagiabhinav.test"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services-location:8.4.0'
}
My Fragment Code
package com.tyagiabhinav.test;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.location.places.ui.PlacePicker;
/**
* Created by abhinavtyagi on 18/03/16.
*/
public class MainFragment extends Fragment implements ConnectionCallbacks, OnConnectionFailedListener {
private View rootView;
private GoogleApiClient mGoogleApiClient;
private boolean isLocationServiceConnected = false;
private static final String LOG_TAG = MainFragment.class.getSimpleName();
private static final int PLACE_PICKER_REQUEST = 7;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.main_fragment, container, false);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
}
Button btn = (Button) rootView.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "Clicked");
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Log.d(LOG_TAG, "Ask for Permission");
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, PLACE_PICKER_REQUEST);
} else {
// No explanation needed, we can request the permission.
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, PLACE_PICKER_REQUEST);
}
} else {
Log.d(LOG_TAG, "Permission Available");
placePicker();
}
}
});
return rootView;
}
private void placePicker() {
if (isLocationServiceConnected) {
Log.d(LOG_TAG, "Connected to location service");
PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
try {
Intent intent = intentBuilder.build(getActivity());
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
} else {
Log.d(LOG_TAG, "Not connected to location service");
Toast.makeText(getActivity(), "Not connected to location service", Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
Log.d(LOG_TAG, "onRequestPermissionsResult");
switch (requestCode) {
case PLACE_PICKER_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(LOG_TAG, "Permission Granted");
// permission was granted, yay! Do the
// contacts-related task you need to do.
placePicker();
} else {
Log.d(LOG_TAG, "Permission Denied");
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getActivity(), "Location Permission is required for accessing Place Picker!", Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
@Override
public void onStart() {
super.onStart();
if (mGoogleApiClient != null)
mGoogleApiClient.connect();
}
@Override
public void onStop() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
super.onStop();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(LOG_TAG, "Location Connection Failed");
isLocationServiceConnected = false;
}
@Override
public void onConnected(Bundle bundle) {
Log.d(LOG_TAG, "Location Service Connected");
isLocationServiceConnected = true;
}
@Override
public void onConnectionSuspended(int i) {
Log.d(LOG_TAG, "Location Service Suspended");
isLocationServiceConnected = false;
}
}
Was putting meta-data tag wrongly
corrected manifest file :P
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tyagiabhinav.test">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="KEY-REMOVED-ON-STACKOVERFLOW" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这篇关于启动后关闭Pick Picker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!