gpt4 book ai didi

try catch 未处理的 Android 地理编码器错误

转载 作者:行者123 更新时间:2023-11-29 18:12:46 25 4
gpt4 key购买 nike

我正在制作一个显示长 lat off 插入地址的应用程序。

它工作正常,但问题仅在于,当我在文本框中插入错误的地址名称(如“abc xyz”)时,它停止并在模拟器上显示错误“不幸的是,位置已停止。”

我无法理解这一点,因为据我所知,try 和 catch 可以处理任何异常。是真的吗????

我的示例项目中只有一个文本框和编辑框,这是按钮单击的代码。这是一个代码...

  try{
bt.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
try {
//List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
add=et.getText().toString();
List<Address> addresses = geocoder.getFromLocationName(add, 1);


if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append("Latitude="+addresses.get(0).getLatitude()).append("\n");
strReturnedAddress.append("Longitude="+addresses.get(0).getLongitude()).append("\n");
strReturnedAddress.append("Country="+addresses.get(0).getCountryName()).append("\n");
strReturnedAddress.append("State="+addresses.get(0).getAdminArea()).append("\n");

}
myAddress.setText(strReturnedAddress.toString());
}
else{
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
myAddress.setText("Canont get Address!");
}
/*Intent intent=new Intent(Main.this, Horoscope.class);
intent.putExtra("extra", myAddress.getText().toString());
startActivity(intent);*/
}
});
}
catch (Exception e) {
// TODO: handle exception
myAddress.setText("Canont get Address!");
}

最佳答案

try{ 
//List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
add=et.getText().toString();
List<Address> addresses = geocoder.getFromLocationName(add, 1);


if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append("Latitude="+addresses.get(0).getLatitude()).append("\n");
strReturnedAddress.append("Longitude="+addresses.get(0).getLongitude()).append("\n");
strReturnedAddress.append("Country="+addresses.get(0).getCountryName()).append("\n");
strReturnedAddress.append("State="+addresses.get(0).getAdminArea()).append("\n");

}
myAddress.setText(strReturnedAddress.toString());
}
else{
myAddress.setText("No Address returned!");
}

}} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
myAddress.setText("Canont get Address!");
}
/*Intent intent=new Intent(Main.this, Horoscope.class);
intent.putExtra("extra", myAddress.getText().toString());
startActivity(intent);*/
}
});
}

catch (Exception e) {
Toast.makeText(getApplicationContext(), "UR MESSAGE" , Toast.LENGTH_LONG).show();
startActivity(new Intent(this, YourActivity.class));
finish();
}

关于try catch 未处理的 Android 地理编码器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com