我正在實施一個小應用程序來獲取當前位置並使用「Cityname」和「Subthrough」值來實現Locationlistener爲myLocationListener。但我得到這些空值。我不知道,爲什麼我將「Cityname」和「SubThrough」值設爲空
Email.java package com.example.nirbhaya;
public class Email extends Activity{
Button buttonSend;
EditText textTo;
EditText textSubject;
EditText textMessage;
SharedPreferences DefaultData;
boolean GPS,flag;
String cityName=null;
String SubThorugh = null;
Intent i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email);
buttonSend = (Button) findViewById(R.id.bgpsYes);
textTo = (EditText) findViewById(R.id.editTextTo);
textSubject = (EditText) findViewById(R.id.editTextSubject);
textMessage = (EditText) findViewById(R.id.editTextMessage);
DefaultData = getSharedPreferences("defMobileNo",0);
final String defTo = DefaultData.getString("defEMail","Couldn't load data");
i = getIntent();
GPS = i.getBooleanExtra("GPSneed", false);
if(GPS)
{
flag = displayGpsStatus();
if (flag)
{
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
}
else
{
alertbox("Gps Status!!", "Your GPS is: OFF");
}
}
String text = "Hi,\n\n \t currently i am at " + SubThorugh +", "+cityName;
textTo.setText(defTo);
textMessage.setText(text);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
});
}
private class mylocationlistener implements LocationListener
{
@Override
public void onLocationChanged(Location location)
{
Log.d("LOCATION CHANGED", "mylocationlistener");
if (location != null)
{
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> addresses;
try
{
addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
cityName=addresses.get(0).getLocality();
SubThorugh = addresses.get(0).getSubThoroughfare();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
/*----------Method to Check GPS is enable or disable ------------- */
private Boolean displayGpsStatus()
{
Log.d("SubThorugh: ", "displayGpsStatus");
ContentResolver contentResolver = getBaseContext().getContentResolver();
boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(
contentResolver, LocationManager.GPS_PROVIDER);
if (gpsStatus) {
return true;
} else {
return false;
}
}
/*----------Method to create an AlertBox ------------- */
protected void alertbox(String title, String mymessage)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your Device's GPS is Disable")
.setCancelable(false)
.setTitle("** Gps Status **")
.setPositiveButton("Gps On",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent myIntent = new Intent(
Settings.ACTION_SECURITY_SETTINGS);
startActivity(myIntent);
dialog.cancel();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the dialog box
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
在上面的代碼我得到
空值在
字符串文本
= 「您好,\ n \ n \噸目前我在」 + SubThorugh + 「」 +的cityName ;
線
,但我在
的System.out.println得到在myClassListener類正確的值(addresses.get(0).getLocality());
請幫助 感謝,
注:我刪除了一些行here.If需要我發佈完整的代碼。
我初始化字符串的cityName = NULL; String SubThorugh = null;作爲全局變量 – KCRaju