0

我正在实现一个小型应用程序来获取当前位置,并为此使用“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();
    }

}

在上面的代码中,我得到

String text = "Hi,\n\n \t 目前我在 " + SubThorugh +", "+cityName;

线

但我在myClassListener类中得到了正确的值

System.out.println(addresses.get(0).getLocality());

请帮忙谢谢,

注意:我在这里删除了一些行。如果需要我发布完整的代码。

4

3 回答 3

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");
        }
    }
于 2013-06-02T16:34:27.757 回答
0

您在onCreate方法中得到null ,因为 line with较早到达,然后方法onLocationChanged被调用,因此SubThorughcityName仍然为nullString text = "Hi,\n\n \t currently i am at " + SubThorugh +", "+cityName;

于 2013-06-02T15:49:51.367 回答
0

您正在使用

String text = "Hi,\n\n \t currently i am at " + SubThorugh +", "+cityName;

在 oncreate 方法。在此之前没有其他初始化。所以 SubThrough 的值是它的初始值 null

于 2013-06-02T15:33:16.400 回答