Android地理编码器错误,它不是由try catch处理的

时间:2012-03-21 10:42:37

标签: android geolocation

我正在创建一个显示长插入地址的应用程序。

它工作正常,但问题只是,当我在文本框中插入错误的地址名称如“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!");
    }

2 个答案:

答案 0 :(得分:2)

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();
}

答案 1 :(得分:1)

试试这个

try
{
 Address returnedAddress = addresses.get(0);
 }
 catch (Exception e) {
 Toast.makeText(getApplicationContext(),"UR MESSAGE",Toast.LENGTH_LONG).show();
 startActivity(new Intent(this, YourActivity.class));
 finish();
 }

我希望你能得到它..