【发布时间】:2015-10-10 08:28:33
【问题描述】:
我在 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() 里面的日志没有打印出来。
奇怪的是来自 GitHub 的 android-place-picker 示例运行良好。
我尝试了以下方法:
- 清理项目
- 清除数据并在设备上重新安装
- IDE 清除缓存并重新启动
有什么想法吗?
--编辑-- 清单.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>
【问题讨论】:
-
您是否在 Google Developer Console 中启用了 Places API for Android
-
当然有。如果未启用 PlacePicker,则不会显示。
-
我也可以显示您的 Manifest.xml 文件
-
requestCode的值是多少 -
您是否还可以添加为我们提供 1) PLACE_PICKER 声明 2) Log.i() 的输出?
标签: android android-fragments google-places-api onactivityresult