我正在尝试创建一个简单的函数,在其中可以从邮政编码中查找信息。在下面提供的代码中,如果我输入有效的邮政编码,则可以正常使用。但是问题出在我输入错误的邮政编码时。我尝试使用语句try catch,但没有显示错误,而是整个应用程序崩溃了。我也想知道如何仅在英国限制搜索。通过邮编而不是使用getfromlocationName函数查找地址是否有更好的选择。
public void finder(String query){
Geocoder geocoder;
List<Address> addresses = new ArrayList<Address>();
geocoder = new Geocoder(this, Locale.getDefault());
try {
//addresses = geocoder.getFromLocation(51.283973, 1.064928, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
addresses = geocoder.getFromLocationName(query,1);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Fail " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
String street = addresses.get(0).getThoroughfare();
Log.i("I0",street);
String city = addresses.get(0).getLocality();
Log.i("I1",city);
String county = addresses.get(0).getSubAdminArea();
Log.i("I2",county);
String country = addresses.get(0).getCountryName();
Log.i("I3",country);
String postalCode = addresses.get(0).getPostalCode();
Log.i("I4",postalCode);
display.setText(street + "\n" + city + "\n" + country + "\n" + postalCode ); }
错误是
07-13 21:15:26.717 10744-10744/com.example.mandeep.testing I/i1: ct3456
07-13 21:15:26.856 10744-10744/com.example.mandeep.testing D/AndroidRuntime: Shutting down VM
07-13 21:15:26.857 10744-10744/com.example.mandeep.testing E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mandeep.testing, PID: 10744
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.mandeep.testing.MainActivity.finder(MainActivity.java:85)
at com.example.mandeep.testing.MainActivity$1.onQueryTextSubmit(MainActivity.java:47)
at android.widget.SearchView.onSubmitQuery(SearchView.java:1197)
at android.widget.SearchView.access$900(SearchView.java:92)
at android.widget.SearchView$8.onEditorAction(SearchView.java:1175)
at android.widget.TextView.onEditorAction(TextView.java:4473)
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:139)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:304)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5219)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
07-13 21:15:26.878 10744-10744/? I/Process: Sending signal. PID: 10744 SIG: 9
我试过了
if(addresses != null && addresses.size() > 0) {
String street = addresses.get(0).getThoroughfare();
Log.i("I0", street);
String city = addresses.get(0).getLocality();
Log.i("I1", city);
String county = addresses.get(0).getSubAdminArea();
Log.i("I2", county);
String country = addresses.get(0).getCountryName();
Log.i("I3", country);
String postalCode = addresses.get(0).getPostalCode();
Log.i("I4", postalCode);
display.setText(street + "\n" + city + "\n" + country + "\n" + postalCode);
}
else{
Toast.makeText(MainActivity.this, "No Results Found", Toast.LENGTH_SHORT).show();
}
仍然是同样的问题。
我已经编辑了代码
public void finder(String query){
Geocoder geocoder;
List<Address> addresses = new ArrayList<Address>();
geocoder = new Geocoder(this, Locale.getDefault());
try {
//addresses = geocoder.getFromLocation(51.283973, 1.064928, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
addresses = geocoder.getFromLocationName(query,1);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Fail " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
Log.i("yes",addresses.size() + "");
if(addresses != null && addresses.size() > 0) {
Log.i("yes","checked");
String street = addresses.get(0).getThoroughfare();
Log.i("I0", street);
String city = addresses.get(0).getLocality();
Log.i("I1", city);
String county = addresses.get(0).getSubAdminArea();
Log.i("I2", county);
String country = addresses.get(0).getCountryName();
Log.i("I3", country);
String postalCode = addresses.get(0).getPostalCode();
Log.i("I4", postalCode);
display.setText(street + "\n" + city + "\n" + country + "\n" + postalCode);
}
else{
Toast.makeText(MainActivity.this, "No Results Found", Toast.LENGTH_SHORT).show();
}
}
现在的错误是
07-13 22:21:02.782 32088-32088/com.example.mandeep.testing I/i1: ct28dx
07-13 22:21:02.942 32088-32088/com.example.mandeep.testing I/yes: 1
07-13 22:21:02.942 32088-32088/com.example.mandeep.testing I/yes: checked
07-13 22:21:02.942 32088-32088/com.example.mandeep.testing D/AndroidRuntime: Shutting down VM
07-13 22:21:02.944 32088-32088/com.example.mandeep.testing E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mandeep.testing, PID: 32088
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.i(Log.java:160)
at com.example.mandeep.testing.MainActivity.finder(MainActivity.java:93)
at com.example.mandeep.testing.MainActivity$1.onQueryTextSubmit(MainActivity.java:52)
at android.widget.SearchView.onSubmitQuery(SearchView.java:1197)
at android.widget.SearchView.access$900(SearchView.java:92)
at android.widget.SearchView$8.onEditorAction(SearchView.java:1175)
at android.widget.TextView.onEditorAction(TextView.java:4473)
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:139)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:304)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5219)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
07-13 22:21:02.973 32088-32088/com.example.mandeep.testing I/Process: Sending signal. PID: 32088 SIG: 9
最佳答案
您应该先检查阵列的大小
if(addresses.size() > 0){
String street = addresses.get(0).getThoroughfare();
Log.i("I0",street);
String city = addresses.get(0).getLocality();
Log.i("I1",city);
String county = addresses.get(0).getSubAdminArea();
Log.i("I2",county);
String country = addresses.get(0).getCountryName();
Log.i("I3",country);
String postalCode = addresses.get(0).getPostalCode();
Log.i("I4",postalCode);
display.setText(street + "\n" + city + "\n" + country + "\n" + postalCode );
}
您正在尝试从索引0获取数据,但列表为空。