簡體   English   中英

從另一個類調用變量時失敗。 (JAVA)

[英]Failed when calling variable from another class. (JAVA)

我是Java編程的新手。 我在這里遇到了問題,任何人都可以發表評論嗎? 當我檢查onLocationChanged() lngpointlatpoint是理想的值。 但是,當我將其調用到另一個方法時, loadcoordinate()loc.latpointloc.lngpoint將變為零嗎? 為什么?

編輯..基本上我的代碼是這樣的:

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));
}

在另一堂課中:

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
}
}

}

我相信MyLocationListener loc應該在類范圍內而不是在構造函數中定義,像這樣

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

我認為問題在於:

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

您打算這樣做嗎?:

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

另外,您是否打算在代碼的第一行大寫“ Public”? 那甚至有效嗎

嘗試以下代碼:

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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM