1
EditText location_tf = (EditText) findViewById(R.id.mapsearchaddress);
String location = location_tf.getText().toString();
//Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
addressList = geocoder.getFromLocationName(location, 1);
}
catch (IOException e) {
Toast.makeText(MapsActivity.this, "Addess not found", Toast.LENGTH_SHORT).show();
}
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
else {
Toast.makeText(MapsActivity.this, "Can't find address", Toast.LENGTH_SHORT).show();
}
}
'如果(位置= NULL ||! location.equals(「」))'應該是'if(location!= null &&!location.equals(「」))' –
發佈logcat錯誤信息 –
java.lang.NullPointerException:嘗試從字段'double com .google.android.gms.maps.model.LatLng.latitude'放在空對象引用上 –