简体   繁体   English

从另一个类调用变量时失败。 (JAVA)

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

I am newbie for java programming. 我是Java编程的新手。 I met some problem here, can anyone give some comment? 我在这里遇到了问题,任何人都可以发表评论吗? When I check in the onLocationChanged() . 当我检查onLocationChanged() There is desired value for lngpoint and latpoint . lngpointlatpoint是理想的值。 But when I called it to another method, loadcoordinate() , loc.latpoint and loc.lngpoint will become zero? 但是,当我将其调用到另一个方法时, loadcoordinate()loc.latpointloc.lngpoint将变为零吗? 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 我相信MyLocationListener loc应该在类范围内而不是在构造函数中定义,像这样

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? 另外,您是否打算在代码的第一行大写“ Public”? 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个类调用类函数。 错误 - Calling class function from another class. Error 从另一个类向标签添加文本。 Java的 - Adding text to a label from another class. Java Java,从类的两个对象调用的方法。 同一类中的一个对象,另一类中的一个对象 - Java , method calling from two objects of a class. One object in the same class and one object in other class 另一个JLabel的setText class. Java - setText of JLabel in another class. Java 从另一个类调用方法时遇到问题。 程序要么无法编译,要么返回空值 - Having trouble calling method from another class. The program either won't compile or returns a null value 需要在一个从另一个类扩展的类中设置一个变量。 或覆盖类型 - Need to set a variable in a class which is extended from another class. Or override the type 将变量从参数中的一个类发送到另一个类。 简单错误 - Send a variable from one class in a parameter to another class. Simple error Java:调用方法并从另一个类实例化对象时出现问题 - Java: Problems when calling methods and instantiating objects from another class 从另一个类调用方法时,Java 找不到符号 - Java cannot find symbol when calling a method from another class 从另一个 class 调用 class 中的局部变量 - Calling a local variable in a class from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM