我在一个ViewPager中有一个Fragment (support.v4),它调用PlacePicker UI,然后在TextView上更新。我的代码如下,
public void openPlacePicker() {
try {
PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
Intent intent = intentBuilder.build(getContext());
// Start the Intent by requesting a result, identified by a request code.
startActivityForResult(intent, PLACE_PICKER);
} catch (GooglePlayServicesRepairableException e) {
GooglePlayServicesUtil
.getErrorDialog(e.getConnectionStatusCode(), getActivity(), 0);
} catch (GooglePlayServicesNotAvailableException e) {
Toast.makeText(getContext(), "Google Play Services is not available.",
Toast.LENGTH_LONG)
.show();
}
}
OnActivityResult是
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Request Code:" + requestCode);//This is printed
if(requestCode == PLACE_PICKER){
Log.d(TAG,"Reached in if()"); //Never printed
}
switch (requestCode) { //Never reached
case PLACE_PICKER:
if (resultCode == Activity.RESULT_OK) {
final Place place = PlacePicker.getPlace(data, getContext());
final CharSequence name = place.getName();
//EventBus.getDefault().post(new NotifyEvent(NotifyEvent.NotifyType.PLACE_PICKED, (String) name));
}
}
}
问题是,条件语句永远不会被执行。打印if()之前的日志。但是if()中的Log不会被打印出来。
奇怪的是android-place-picker样本从GitHub工作得很好。我已经试过了:
- 清洁项目
- 清除数据并在设备上重新安装
- IDE清空缓存并重启
任何想法?
——编辑Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.apps.maps">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.apps.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission android:name="com.apps.maps.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<application android:name=".MyApplication" 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="@string/google_maps_key" />
<activity android:name=".activity.MainActivity" android:label="@string/title_activity_maps">
</activity>
</application>
</manifest>
我知道这是一个较旧的线程,但它帮助我解决了与原始问题类似的问题。Raman Bhavsar的10/10/15评论是一个正确的问题。我忘记在Google Developer Console中启用Places API,一旦我启用了,我就能够在onActivityResult方法中遇到断点。