我正在实现一个小应用程序来获取当前位置并使用“Cityname”和“Subthrough”值,因为我正在为 myLocationListener 实现 Locationlistener 。但是我得到了那些空值。
Email.java 包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();
}
}
在上面的代码中,我在
获得 null 值String text =“嗨,\ n \ n \ t目前我在”+ SubThorugh +“,”+ cityName;
线
但我在 myClassListener 类
中获得了正确的值的System.out.println(addresses.get(0).getLocality());
请帮忙 感谢,
注意:我在这里删除了一些行。如果需要我发布完整代码。
答案 0 :(得分:0)
您正在使用
String text = "Hi,\n\n \t currently i am at " + SubThorugh +", "+cityName;
在oncreate方法。之前没有其他初始化。所以SubThrough的值是它的初始值null
答案 1 :(得分:0)
您在 onCreate 方法中获得 null ,因为先前已达到String text = "Hi,\n\n \t currently i am at " + SubThorugh +", "+cityName;
行,然后调用方法 onLocationChanged ,因此 SubThorugh 和 cityName 仍然 null 。
答案 2 :(得分:0)
以这种方式试用你的代码
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);
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) {
});
}
else
{
alertbox("Gps Status!!", "Your GPS is: OFF");
}
}