2012-03-12 69 views
2

我正在编写一个应用程序,需要显示地址以便日后可以使用。 我从LocationManager获取位置,并且想要显示按下时显示地址的Toast。我的问题是,按下按钮时,什么也没有发生。 (甚至在logcat中)将当前地址显示为吐司

下面的代码:

Button getTaxi = (Button) findViewById(R.id.GetTaxi); 
getTaxi.setOnClickListener(new View.OnClickListener() { 

    public void getAddress() { 
     Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault()); 
     String result = null; 
     try { 
      List<Address> list = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); 
      if (list != null && list.size() > 0) { 
       Address address = list.get(0); 
       // sending back first address line and locality 
       result = address.getAddressLine(0) + ", " + address.getLocality(); 
      } 
     } catch (IOException e) { 
      String exception = String.format("IOException!!"); 
      Toast.makeText(getApplicationContext(), exception, Toast.LENGTH_LONG); 
     } finally { 
      if (result != null) { 
       Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG); 
      } else 
       Toast.makeText(getApplicationContext(), "nix is", Toast.LENGTH_LONG); 
     } 

     final Intent intent = new Intent(); 
     setResult(RESULT_OK, intent); 
     finish(); 
     return; 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
    } 
}); 

我知道这是真的propably混乱,但我刚开始JAVA/Android的编程。

非常感谢你提前!

回答

5

而是执行此操作:

Button getTaxi = (Button) findViewById(R.id.GetTaxi); 
getTaxi.setOnClickListener(new View.OnClickListener() { 


    Toast toast; 

    public void getAddress() { 
     Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault()); 
     String result = null; 
     try { 
      List<Address> list = geocoder.getFromLocation(
        location.getLatitude(), location.getLongitude(), 1); 
      if (list != null && list.size() > 0) { 
       Address address = list.get(0); 
       // sending back first address line and locality 
       result = address.getAddressLine(0) + ", " + address.getLocality(); 
      } 
     } catch (IOException e) { 
      String exception = String.format("IOException!!"); 
      toast = Toast.makeText(getApplicationContext(), exception, Toast.LENGTH_LONG); 
     } finally { 

      if (result != null) { 
       toast = Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG); 
      } else 
       toast = Toast.makeText(getApplicationContext(), "nix is", Toast.LENGTH_LONG); 
     } 




     final Intent intent = new Intent(); 
     setResult(RESULT_OK, intent); 
     finish(); 
     return; 
    } 


    @Override 
    public void onClick(View v) { 
     getAddress(); 
     toast.show(); 
    } 
}); 
+0

谢谢你这么mutch!这帮助我很多! – Teonee 2012-03-12 04:29:33

2

getAddress未被调用。在onClick方法中添加getAddress()。

2

public void onClick(View v)需要有代码,因为这是按下按钮时执行的代码。就你而言,看起来你可以简单地在onClick()内拨打getAddress()

0

你必须做出的onclick梅托德,然后你必须通过的getAddress() ,那么你可以很容易地使addreess当u点击后按钮