Java 应用程序崩溃,因为我正在搜索我的ArrayList中不存在的索引。我怎样才能修好它?
我已经阅读了所有关于IOException的文档,以及它们产生时的含义。我知道我正在ArrayList中搜索不存在的en索引,从而生成Java 应用程序崩溃,因为我正在搜索我的ArrayList中不存在的索引。我怎样才能修好它?,java,android,exception,indexoutofboundsexception,Java,Android,Exception,Indexoutofboundsexception,我已经阅读了所有关于IOException的文档,以及它们产生时的含义。我知道我正在ArrayList中搜索不存在的en索引,从而生成索引AutofBoundsException:Index:0,Size:0。我查阅了以下文档和帖子,试图自己解决这个问题,但没有成功。在做了彻底的研究之后,我希望有人能帮助我避开这个问题。我在这里合法地浏览了许多类似问题的帖子,试图单独解决我的错误,但未能得出正确的结论 。 例如,我尝试在地址列表周围放置enif语句,并发出toast消息警告用户必须优化搜索。当
索引AutofBoundsException:Index:0,Size:0
。我查阅了以下文档和帖子,试图自己解决这个问题,但没有成功。在做了彻底的研究之后,我希望有人能帮助我避开这个问题。我在这里合法地浏览了许多类似问题的帖子,试图单独解决我的错误,但未能得出正确的结论
。
例如,我尝试在地址列表
周围放置enif
语句,并发出toast
消息警告用户必须优化搜索。当搜索地图上的大多数地方时,结果会很好,并且会在地图上的特定位置放置一个标记。当我搜索Paris时,我得到了一个异常,就像我说的,我已经将它包装在一个if
语句和其他方法中,但没有成功
searchView.setOnQueryTextListener(新的searchView.OnQueryTextListener(){
@凌驾
公共布尔值onQueryTextSubmit(字符串查询){
字符串位置=searchView.getQuery().toString();
List addressList=null;
if(location!=null | |!location.equals(“”){
Geocoder Geocoder=新的地理编码器(MapsActivityPublisher.this);
试一试{
addressList=geocoder.getFromLocationName(位置,1);
}捕获(IOE异常){
e、 printStackTrace();
}
地址=地址列表。获取(0);
LatLng LatLng=新LatLng(address.getLatitude(),address.getLongitude());
addMarker(新的MarkerOptions().position(latLng).title(location).draggable(true));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,10));
}
返回false;
}
@凌驾
公共布尔onQueryTextChange(字符串newText){
返回false;
}
});
Logcat
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at com.e.events.Map.MapsActivityPublisher$1.onQueryTextSubmit(MapsActivityPublisher.java:62)
at android.widget.SearchView.onSubmitQuery(SearchView.java:1270)
at android.widget.SearchView.access$1000(SearchView.java:99)
at android.widget.SearchView$7.onEditorAction(SearchView.java:1247)
at android.widget.TextView.onEditorAction(TextView.java:6453)
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:363)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
在从
ArrayList
if (location != null && !location.equals("")) {
Geocoder geocoder = new Geocoder(MapsActivityPublisher.this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
if(addressList != null && !addressList.isEmpty()) {
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
map.addMarker(new MarkerOptions().position(latLng).title(location).draggable(true));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
}
}
我试着在地址列表中放置en-if语句-您的代码中没有这方面的内容sort@user7我把它脱了,因为它坏了。。。我在
Address Address=…
周围做了if
语句,但应用程序一直崩溃,所以我把它从这个or(|
)语句中去掉,它应该是and(&&
),因为您想检查这两种情况:如果(location!=null | | | location.equals(“)
)。当前一个空字符串将通过第一次检查,因此不会接受第二次检查。@JakeSteam刚刚尝试过。。。仍然需要显示更多的代码<代码>地理编码器.getFromLocationName(位置,1)正在抛出错误或返回空列表。为什么&
而不是|
?|
允许null
和empty
执行代码。如果null
第一个条件位置!=空
返回假
但!location.equals(“”)返回true
。因此,如果为空
第一个条件位置!=空
返回真
。所以代码块executeOkay。我只需要其中一个条件为true,然后两者都返回一个有效的查询。但是谢谢。现在好像在工作