Android | Google PlacePicker API。 OnActivityResult代码未执行[真奇怪的行为]

时间:2015-10-10 08:28:33

标签: android android-fragments google-places-api onactivityresult

我在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清除缓存并重新启动

有什么想法吗?

- EDIT-- 的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>

1 个答案:

答案 0 :(得分:0)

我知道这是一个较旧的帖子,但它帮助我解决了原始问题的类似问题。 Raman Bhavsar的10/10/15评论是正确的问题。我忘了在Google Developer Console中启用Places API,一旦我这样做,我就可以在onActivityResult方法中点击断点。