【发布时间】: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