我正在编写一个应用程序并需要显示地址,以便我以后可以使用它。 我从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
}
});
我知道它可能非常混乱,但我刚开始使用JAVA / Android编程。
提前非常感谢你!
答案 0 :(得分: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();
}
});
答案 1 :(得分:2)
未调用getAddress。在onClick方法中添加getAddress()。
答案 2 :(得分:2)
public void onClick(View v)
需要包含代码,因为这是按下按钮时执行的操作。在您的情况下,看起来您只需在getAddress()
内拨打onClick()
。
答案 3 :(得分:0)
你必须制作onclick metod然后你必须传递getAddress() 然后,当你点击按钮
时,你可以轻松地进行添加