在if (location != null || !location.equals(""))上得到“总是正确”的结果,当我搜索像"sldjfuhsdhfj“这样的东西时,它会关闭应用程序,但当我输入像”中央公园“这样的东西时,它就能正常工作。我做错了什么?我正在使用的代码
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);
}
}发布于 2017-06-05 21:33:02
地址= addressList.get(0)
您给了它垃圾信息,所以它没有返回任何结果。addressList为空。您正在访问某个空元素的0元素。您应该已经在Android Studio的Android Monitor窗口中看到了一条消息,指出了这一行/问题。
https://stackoverflow.com/questions/38863278
复制相似问题