Android Google places api无法在新api中自动完成搜索查询

Android Google places api无法在新api中自动完成搜索查询,android,google-maps,google-places-api,Android,Google Maps,Google Places Api,我正在使用AsyncTask获取结果。我在doinbackground方法中得到一个错误,表示它试图对空对象引用调用方法 这是我的密码 私有类PlaceListTask扩展了AsyncTask>{ private String strAddress; public PlaceListTask(String strAddress) { super(); this.strAddress = strAddress; } public

我正在使用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;
    }
私人服装;
公共PlaceListTask(字符串){
超级();
this.strAddress=strAddress;
}
公共字符串getStradAddress(){
返回服装;
}
公共连衣裙(弦乐连衣裙){
this.strAddress=strAddress;
}
@凌驾
受保护的ArrayList doInBackground(字符串…参数){
List placeFields=Arrays.asList(Place.Field.ID,Place.Field.NAME/*根据需要添加更多Place.Field.fields*/);
FindCurrentPlaceRequest请求=
FindCurrentPlaceRequest.newInstance(placeFields);
Task placeResponse=placesClient.findCurrentPlace(请求);
placeResponse.addOnCompleteListener(任务->{
if(task.issusccessful()){
FindCurrentPlaceResponse=task.getResult();
for(placelikelike:response.getplacelikelikelihoods()){
Log.i(TAG,String.format(“位置“%s”的可能性为:%f”),
place.getPlace().getName(),
placelikelibility.getlikelibility());
}
}否则{
Exception=task.getException();
if(ApiException的异常实例){
ApiException=(ApiException)异常;
Log.e(标记“未找到位置:”+apiException.getStatusCode());
}
}
});
返回null;
}
这就是我得到的错误
在调用PlacesClient.findCurrentPlace(请求)之前,尝试在空对象引用上调用接口方法“com.google.android.gms.tasks.Task com.google.android.libraries.places.places.api.net.FindCurrentPlaceRequest”检查placesClient是否为空。

您遇到了什么错误?我已将错误添加到问题中,您是否已正确检查API密钥以查找位置?我已检查,其正确性这可能有助于您连接,但找不到任何位置1.您使用的是真实设备吗?2.您是否像这样初始化placeClient:placesClient=Places.createClient(context)我使用的是一个真实的设备,api已成功初始化,但当我搜索它时,记录了未找到的位置,该位置不应出现在
findcurrentsplacerequest findcurrentsplacerequest=findcurrentsplacerequest.builder(placeFields).build();
而不是:
FindCurrentPlaceRequest=FindCurrentPlaceRequest.newInstance(placeFields);
它不起作用。我只是试图将不推荐使用的api转换为新的PlaceAPI。这就是我遇到此类问题的原因