Android 错误:不能在UI线程上调用wait
我有一个EditText,它有助于自动完成谷歌地图的位置。 这是一个错误:Android 错误:不能在UI线程上调用wait,android,google-maps,Android,Google Maps,我有一个EditText,它有助于自动完成谷歌地图的位置。 这是一个错误: await must not be called on the UI thread. 更改EditText中的文本时,将调用AutoCompletePlace API。 我怎样才能解决这个问题 @Override public void onTextChanged(CharSequence s, int start, int before, int count) { try {
await must not be called on the UI thread.
更改EditText中的文本时,将调用AutoCompletePlace API。我怎样才能解决这个问题
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
GeoDataApi geoDataApi = Places.GeoDataApi;
PendingResult<AutocompletePredictionBuffer> autocompletePredictions = geoDataApi.
getAutocompletePredictions(GoogleClient.getIstance(), searchLoc.getText().toString(), null, null);
AutocompletePredictionBuffer autocompletePredictionBuffer= autocompletePredictions.await();
System.out.println("Size: " + autocompletePredictionBuffer.getCount());
for(int i=0; i<autocompletePredictionBuffer.getCount(); i++){
Place p= (Place) geoDataApi.
getPlaceById(GoogleClient.getIstance(), autocompletePredictionBuffer.get(i).getPlaceId());
searchList.add(
new SearchListProvider(
R.mipmap.ic_launcher,
autocompletePredictionBuffer.get(i).getFullText(null).toString(),
p)
);
}
}
catch (Exception e){
e.printStackTrace();
System.out.println("Message: "+e.getMessage());
Log.d("Message: ", e.getMessage());
}
Toast.makeText(getContext(), searchLoc.getText(), Toast.LENGTH_SHORT).show();
}
@覆盖
public void onTextChanged(字符序列、int start、int before、int count){
试一试{
GeoDataApi GeoDataApi=Places.GeoDataApi;
Pendingreult autocompletePredictions=地理数据API。
getAutocompletePredictions(GoogleClient.getIstance(),searchLoc.getText().toString(),null,null);
AutocompletePredictionBuffer AutocompletePredictionBuffer=autocompletePredictions.await();
System.out.println(“大小:“+autocompletedpredictionbuffer.getCount());
对于(int i=0;i我认为您应该在这里使用AsyncTask。因为await正在停止当前线程。所以您应该将请求发送到工作线程中的API。然后您可以在onPostExecute()中更新结果。AutocompletePredictionBuffer AutocompletePredictionBuffer=autocompletePredictions.await();----我应该在异步任务中执行此操作。@ShubhamAgarwalBhewanewala如果您将所有相关的逻辑放入doInBackground中就可以了。在我的活动的onCreate()中调用OptionalPendingResult.await()
,我遇到了类似的问题
method。将其移动到AsyncTask
解决了这个问题。对于上下文,我有一个活动,它执行一组检查,以确定用户和应用程序处于何种状态,并将它们转发到正确的活动。其中一部分是使用GoogleSignInApi.silentSignIn()的Google登录静默身份验证检查
这要求您使用wait()
。