2012-03-21 8 views
1

挿入されたアドレスが長時間表示されないアプリケーションを1つ作成しています。Androidジオコーダーtry catchで処理されないエラー

正常に動作しますが、問題はそれだけです。テキストボックスに "abc xyz"のような間違ったアドレス名を挿入すると停止し、 "残念ながら場所が停止しました"というエラーが表示されます。

私はtryとcatchが例外を処理できることを知っているので、これを理解できません。本当ですか?

私のサンプルプロジェクトには1つのテキストボックスとeditboxしかありませんが、これはボタンクリックのコードです。ここ がコードです...

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!"); 
    } 
+0

..あなたはそれを得るでしょう願っていますが、ジオコーダBEZ 'NullPointerException'と' IOException'をキャッチしようとしてみてくださいこれらの例外をスローする –

答えて

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

この

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

私は

関連する問題