[英]geocoder.getFromLocationName error
在if (location != null || !location.equals(""))
上獲得“始終為真”的結果,當我搜索“ sldjfuhsdhfj”之類的內容時,它將關閉該應用程序,但是當我輸入“ Central Park”之類的內容時,它將起作用正確地。 我究竟做錯了什么? 我正在使用的代碼
public void onMapSearch(View view) {
EditText locationSearch = (EditText) findViewById(R.id.editText1);
String location = locationSearch.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s
mMap.clear();
}
}, 3000);
}
}
地址地址= addressList.get(0)
您給它加了垃圾,因此沒有任何結果。 addressList為空。 您正在訪問空元素的0元素。 您應該已經在Android Studio的“ Android監視器”窗口中看到一條消息,指出了此確切的線路/問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.