简体   繁体   中英

Failed when calling variable from another class. (JAVA)

I am newbie for java programming. I met some problem here, can anyone give some comment? When I check in the onLocationChanged() . There is desired value for lngpoint and latpoint . But when I called it to another method, loadcoordinate() , loc.latpoint and loc.lngpoint will become zero? Why?

Edited..basically my code is like this:

public class Testgpslocation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Button gpsButton = (Button) findViewById(R.id.gpsButton);
    LocationManager a = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    a.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new MyLocationListener());
    gpsButton.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            LoadCoords();
        }});
}
MyLocationListener loc;
public void LoadCoords(){
loc = new MyLocationListener();

    String message = String.format(
            "wao New Location \n Longitude: %1$s \n Latitude: %2$s",
            loc.latpoint, loc.lngpoint
    );
    Toast.makeText(Testgpslocation.this, message, Toast.LENGTH_LONG).show();

    TextView latText = (TextView) findViewById(R.id.latText);
    TextView lngText = (TextView) findViewById(R.id.lngText);


    double latitude = loc.latpoint;
    double longtitude = loc.lngpoint;
    latText.setText(Double.toString(latitude));
    lngText.setText(Double.toString(longtitude));
}

In another class:

private class MyLocationListener implements LocationListener {
    private double latpoint;
 private double lngpoint;

public void onLocationChanged(Location location) {
    this.latpoint = location.getLatitude();
    this.lngpoint = location.getLongitude();
    String message = String.format(
            "New Location \n Longitude: %1$s \n Latitude: %2$s",latpoint, lngpoint
    );
    Toast.makeText(Testgpslocation.this, message, Toast.LENGTH_LONG).show();
}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub
}
}

}

I belive MyLocationListener loc should be defined in the class scope and not in the construtor, like this

Public class A{
 MyLocationListener loc;
    public void loadcoordinate(){
          loc = new MyLocationListener();
        loc.latpoint, loc.lngpoint;
    }
}

I think the issue is on this line:

MyLocationListener loc = new MyLocationListener();
    loc.latpoint, loc.lngpoint;

Did you intend to do this?:

MyLocationListener loc = new MyLocationListener(loc.latpoint, loc.lngpoint);

Also, did you intend to capitalize "Public" on the very first line of your code? Is that even valid

Try the following code:

public class Testgpslocation extends Activity {
/** Called when the activity is first created. */
MyLocationListener loc;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Button gpsButton = (Button) findViewById(R.id.gpsButton);
    LocationManager a = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    loc = new MyLocationListener();
    a.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
    gpsButton.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            LoadCoords();
        }});
}


public void LoadCoords(){


    String message = String.format(
            "wao New Location \n Longitude: %1$s \n Latitude: %2$s",
            loc.latpoint, loc.lngpoint
    );
    Toast.makeText(Testgpslocation.this, message, Toast.LENGTH_LONG).show();

    TextView latText = (TextView) findViewById(R.id.latText);
    TextView lngText = (TextView) findViewById(R.id.lngText);


    double latitude = loc.latpoint;
    double longtitude = loc.lngpoint;
    latText.setText(Double.toString(latitude));
    lngText.setText(Double.toString(longtitude));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM