Java 在android中用祝酒词覆盖IndexOutOfBoundsException?
有一个自动完成文本视图和按钮,当搜索一个位置时,它将显示该位置。然后选择位置并按下按钮将获得从我的位置到搜索位置的路线方向。问题是,当我尝试输入一个错误的条目(如“aweafnnanjndj”)并按下该按钮时,会出现IndexAutofBoundsException并关闭应用程序。任何人都可以帮助修复此错误。有没有可能像那样在那里“错误的地点”或“重新进入地点”敬酒?下面是获取错误的代码Java 在android中用祝酒词覆盖IndexOutOfBoundsException?,java,arraylist,indexoutofboundsexception,Java,Arraylist,Indexoutofboundsexception,有一个自动完成文本视图和按钮,当搜索一个位置时,它将显示该位置。然后选择位置并按下按钮将获得从我的位置到搜索位置的路线方向。问题是,当我尝试输入一个错误的条目(如“aweafnnanjndj”)并按下该按钮时,会出现IndexAutofBoundsException并关闭应用程序。任何人都可以帮助修复此错误。有没有可能像那样在那里“错误的地点”或“重新进入地点”敬酒?下面是获取错误的代码 btnEnd= (Button) findViewById(R.id.tab2); OnCl
btnEnd= (Button) findViewById(R.id.tab2);
OnClickListener findClickListener=new OnClickListener() {
@Override
public void onClick(View v) {
checkingNetwork();
if (isOnline())
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(atvPlaces.getWindowToken(), 0);
atvPlaces.clearFocus();
String location = atvPlaces.getText().toString();
if (location != null && !location.equals(""))
{
new GeocoderTask().execute(location);
}
Geocoder coder = new Geocoder(getApplicationContext());
List<Address> address = null;
try
{
address = coder.getFromLocationName(location, 1);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if (location != null && !location.equals("") && isOnline())
{
locationtest = address.get(0); <----//Line Number 3354//
LatLng fromPosition = new LatLng(
locationtest.getLatitude(),
locationtest.getLongitude());
setMarkerOnLocation(fromPosition);
sLat=locationtest.getLatitude();
sLong=locationtest.getLongitude();
LatLng newPoss=new LatLng(latitude, longitude);
setMarkerOnLocation(newPoss);
}
else
{
Toast.makeText(getApplicationContext(), "Re enter Location", Toast.LENGTH_SHORT).show();
}
}
}
};
btnEnd.setOnClickListener(findClickListener);
您应该在第3354行之前测试
address
是否为null或空,如果是这种情况,则显示toast而不是到达address.get(0)
,类似于:
if (address ==null || address.isEmpty()) {
Toast.makeText(getApplicationContext(), "wrong address",
Toast.LENGTH_LONG).show();
} else {
locationtest = address.get(0);
....
“地址”商店是什么?非常感谢您的解决方案。。已修复错误。。我还有一个问题,google mapv2是否有从4.1.2到4.4的更高版本的支持权限?有时会自动关闭我创建的谷歌地图应用程序。。
if (address ==null || address.isEmpty()) {
Toast.makeText(getApplicationContext(), "wrong address",
Toast.LENGTH_LONG).show();
} else {
locationtest = address.get(0);
....