【问题标题】:Android | Google PlacePicker API. OnActivityResult code not executed [Really weird behaviour]安卓 |谷歌 PlacePicker API。 OnActivityResult 代码未执行 [真的很奇怪的行为]
【发布时间】: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


【解决方案1】:

我知道这是一个较旧的线程,但它帮助我解决了与原始问题类似的问题。 Raman Bhavsar 2015 年 10 月 10 日的评论是正确的问题。我忘记在 Google Developer Console 中启用 Places API,一旦启用,我就能够在 onActivityResult 方法中命中断点。

【讨论】:

    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    相关资源
    最近更新 更多