无法从PlaceBuffer获取特定的Place [英] Fail to obtain specific Place from PlaceBuffer

查看:94
本文介绍了无法从PlaceBuffer获取特定的Place的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个特定的地方有一个奇怪的问题.有什么主意吗?

I have strange problem with one specific place. Any idea ?

这段代码可以正常工作,但是对于一个地方(由于以aaa开头而偶然选择-仅用于测试),它无法从缓冲区中获取Place对象.甚至查询也成功(日志:PlaceAutocompleteAdapt:查询已完成.收到了5条预测.) 不幸的是,它使应用程序崩溃.这会使该API变得非常危险,请确保在try catch块中还要处理什么呢?

This code is working fine, but for one place (accidentally choosen because of starts with aaa - just for test) it fails to obtain Place object from buffer. Even a query was successfull (log: PlaceAutocompleteAdapt﹕ Query completed. Received 5 predictions.) Unfortunatelly, it crash application. It make this API very dangerous, what else should be handled in try catch block for sure?

        addressAutocompleteText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            /*
             Retrieve the place ID of the selected item from the Adapter.
             The adapter stores each Place suggestion in a PlaceAutocomplete object from which we
             read the place ID.
              */
            final PlaceAutocompleteAdapter.PlaceAutocomplete item = ((MainActivity) getActivity()).mAdapter.getItem(position);
            final String placeId = String.valueOf(item.placeId);
            Log.i(TAG, "Autocomplete item selected: " + item.description);

            /* Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place. */
            PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi.getPlaceById(((MainActivity) getActivity()).mGoogleApiClient, placeId);
            placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
                @Override
                public void onResult(PlaceBuffer places) {
                    if (!places.getStatus().isSuccess()) {
                        // Request did not complete successfully
                        Log.e(TAG, "Place query did not complete. Error: " + places.getStatus().toString());
                        places.release();
                        return;
                    }

                    try {
                        // Get the Place object from the buffer.
                        final Place place = places.get(0); // Throws IllegalStateException
                        if (null != place) {
                            coordinate = new Coordinate(place.getLatLng().latitude, place.getLatLng().longitude);
                        }
                    } catch (IllegalStateException e) {
                        Log.w(TAG, "Fail to get place or its coordinates");
                    }

                    places.release();
                }
            });
//                hideKeyboard(addressAutocompleteText);

            Log.i(TAG, "Called getPlaceById to get Place details for " + item.placeId);
        }
    });

日志:

Starting autocomplete query for: aaa
06-05 08:05:29.618    6403-7777/...I/PlaceAutocompleteAdapt﹕ Query completed. Received 5 predictions.
06-05 08:05:39.248    6403-6403/...I/FavoriteLocationCreateFragment﹕ Autocomplete item selected: AAA Auto A.s., Zlín, Česká republika
06-05 08:06:01.278    6403-6403/...I/FavoriteLocationCreateFragment﹕ Called getPlaceById to get Place details for ChIJcYm-YVpzE0cR-OIfKj2KTKc
06-05 08:30:46.823  19727-19727/... D/AndroidRuntime﹕ Shutting down VM
06-05 08:30:46.833  19727-19727/... E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ..., PID: 19727
    java.lang.IllegalStateException
            at com.google.android.gms.common.internal.zzv.zzP(Unknown Source)
            at com.google.android.gms.common.data.zzc.zzaB(Unknown Source)
            at com.google.android.gms.common.data.zzc.<init>(Unknown Source)
            at com.google.android.gms.location.places.internal.zzq.<init>(Unknown Source)
            at com.google.android.gms.location.places.internal.zzo.<init>(Unknown Source)
            at com.google.android.gms.location.places.PlaceBuffer.get(Unknown Source)
            at ....FavoriteLocationCreateFragment$3$1.onResult(FavoriteLocationCreateFragment.java:129)
            at ....FavoriteLocationCreateFragment$3$1.onResult(FavoriteLocationCreateFragment.java:117)
            at com.google.android.gms.common.api.AbstractPendingResult$CallbackHandler.deliverResultCallback(Unknown Source)
            at com.google.android.gms.common.api.AbstractPendingResult$CallbackHandler.handleMessage(Unknown Source)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

推荐答案

添加条件&& places.getCount() > 0

文档示例:

Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
                 .setResultCallback(new ResultCallback<PlaceBuffer>() {
        @Override
        public void onResult(PlaceBuffer places) {
            if (places.getStatus().isSuccess() && places.getCount() > 0) {
            final Place myPlace = places.get(0);
            Log.i(TAG, "Place found: " + myPlace.getName());
        } else {
            Log.e(TAG, "Place not found");
        }
        places.release();
    }
});

这篇关于无法从PlaceBuffer获取特定的Place的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆