嗨我已经在我的应用程序内部制作了一个方法,根据用户输入的编辑文本搜索地址。当编辑文本为空时按下搜索按钮时,此工作完全正常但崩溃。可以找出我可能做错的事情以及如何解决问题。
public void onSearch(View view) {
EditText findLocation = (EditText) findViewById(R.id.editText);
String location = findLocation.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
//Geocode gecoder = new Geocoder(this)
//geocoder.getFromLocationName(location, 1);
//then add try and catch
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
//after try and catch
// List<Address> 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(location.toString()));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
堆栈跟踪
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.hadleigh.googlemapstest.MapsActivity.onSearch(MapsActivity.java:91)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
答案 0 :(得分:1)
您有正确的逻辑来检查EditText
是否为空,但您仍然希望addressList
填满结果。您需要将addMarker
逻辑放在try-catch blocks
内,或检查addressList是否为空:
if (addressList != null && !addressList.isEmpty()) {
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title(location.toString()));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
答案 1 :(得分:0)
public boolean onSearch(View view) {
EditText findLocation = (EditText) findViewById(R.id.editText);
String location = findLocation.getText().toString();
List<Address> addressList = null;
if (location.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter address", Toast.LENGTH_LONG).show();
return true;
}else {
//Geocode gecoder = new Geocoder(this)
//geocoder.getFromLocationName(location, 1);
//then add try and catch
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
//after try and catch
// List<Address> 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(location.toString()));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
return false;
}