【问题标题】:Google places api fails to autocomplete the search query in the new api librarayGoogle Places api 无法自动完成新 api 库中的搜索查询
【发布时间】:2020-06-10 00:21:12
【问题描述】:

我正在使用 AsyncTask 来获取我的结果。我在 doinbackground 方法中收到一个错误,说它试图在空对象引用上调用方法。

这是我的代码 私有类 PlaceListTask 扩展 AsyncTask> {

    private String strAddress;

    public PlaceListTask(String strAddress) {
        super();
        this.strAddress = strAddress;
    }

    public String getStrAddress() {
        return strAddress;
    }

    public void setStrAddress(String strAddress) {
        this.strAddress = strAddress;
    }

    @Override
    protected ArrayList<AutocompletePrediction> doInBackground(String... params) {
  List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME /* add more Place.Field. fields as you need*/);
        FindCurrentPlaceRequest request =
                FindCurrentPlaceRequest.newInstance(placeFields);


            Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request);
            placeResponse.addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    FindCurrentPlaceResponse response = task.getResult();
                    for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
                        Log.i(TAG, String.format("Place '%s' has likelihood: %f",
                                placeLikelihood.getPlace().getName(),
                                placeLikelihood.getLikelihood()));
                    }
                } else {
                    Exception exception = task.getException();
                    if (exception instanceof ApiException) {
                        ApiException apiException = (ApiException) exception;
                        Log.e(TAG, "Place not found: " + apiException.getStatusCode());
                    }
                }
            });
      return null;
    }

这是我得到的错误 尝试调用接口方法 'com.google.android.gms.tasks.Task com.google.android.libraries.places.api.net.PlacesClient.findCurrentPlace(com.google.android.libraries.places.api.net.FindCurrentPlaceRequest )' 在空对象引用上

【问题讨论】:

  • 你遇到了什么错误?
  • 我现在已将错误添加到问题中
  • 你检查 API 密钥是否正确?
  • 我已经检查过了,它是正确的

标签: android google-maps google-places-api


【解决方案1】:

在调用 placesClient.findCurrentPlace(request) 之前,请检查 placesClient 是否为空。

【讨论】:

  • 已连接但找不到任何位置
  • 1.你用的是真机吗? 2. 你是这样初始化placeClient的吗:placesClient = Places.createClient(context)
  • 我使用的是真实设备,并且 api 已成功初始化,但是当我搜索它时,会记录不应该找到的地方
  • 试试FindCurrentPlaceRequest findCurrentPlaceRequest = FindCurrentPlaceRequest.builder(placeFields).build(); 代替:FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);
  • 它没有用。我只是想将已弃用的 API 转换为新的地方 API。这就是我遇到此类问题的原因
猜你喜欢
  • 2020-07-07
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
  • 2014-05-01
  • 2017-10-02
  • 1970-01-01
相关资源
最近更新 更多