如何在android中获得lat和long

如何在android中获得lat和long,android,location,Android,Location,我在代码中遇到了一个小问题,并因此陷入困境。以下是我的代码:- public class MainActivity extends Activity { TextView textView1; Location currentLocation; double currentLatitude,currentLongitude; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sav

我在代码中遇到了一个小问题,并因此陷入困境。以下是我的代码:-

public class MainActivity extends Activity {
TextView textView1;
Location currentLocation;
double currentLatitude,currentLongitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView1 = (TextView) findViewById(R.id.textView1);

    findLocation();

    textView1.setText(String.valueOf(currentLatitude) + "\n"
            + String.valueOf(currentLongitude));

}

 public void findLocation() {

        LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {

            public void onLocationChanged(Location location) {

                updateLocation(location,currentLatitude,currentLongitude);

                Toast.makeText(
                        MainActivity.this,
                        String.valueOf(currentLatitude) + "\n"
                                + String.valueOf(currentLongitude), 5000)
                        .show();

                }

            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
            }
        };

        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

    }


    void updateLocation(Location location,double currentLatitude,double currentLongitude) {
            currentLocation = location;
            this.currentLatitude = currentLocation.getLatitude();
            this.currentLongitude = currentLocation.getLongitude();

        }
}

一切正常。但我的问题是类级别变量currentLatitude和current经度的值为null。在上面的代码中,当我在更新位置方法的文本视图中设置lat和long时,它工作正常,但当我想在创建方法中的文本视图中设置相同的值时,它会给出空值。为什么我不知道。请帮助解决此问题。提前感谢

这是因为在Oncreate方法lat和long中的textview中设置文本时未初始化。它将在更新时初始化

所以您应该在updatelocation()方法中设置文本

locationlistener需要一些时间来更新其lat和long,以便同时执行oncreate方法,从而使lat和long不会更新并保持为null。因此,最好将文本设置为updatelocation


希望能有帮助

我建议您将
textView1.setText(String.valueOf(currentLatitude)+“\n”

+字符串.valueOf(currentLength))updateLocation
函数中执行code>。

查找位置需要一段时间才能返回值。它以异步方式发生,同时UI线程继续

因此,在
onCreate()
中,您还没有位置。在
onCreate()
中调用的顺序并不重要

只有在调用
updateLocation()
方法时,您才能获得位置,并且无法保证在设置视图时会发生这种情况

因此,要解决这个问题,请更新其中的textView文本

void updateLocation(Location location,double currentLatitude,double currentLongitude) {
    currentLocation = location;
    this.currentLatitude = currentLocation.getLatitude();
    this.currentLongitude = currentLocation.getLongitude();
    textView1.setText(String.valueOf(currentLatitude) + "\n"
        + String.valueOf(currentLongitude));
}

updateLocation
-在
onCreate
中设置textview值,该位置尚未找到是的,更新位置需要一些时间。因此在updateLocation()内的textview中设置llat&long@BLaZuRE-因为
findLocation()在放入
文本视图1.set…
之前被调用。谢谢兄弟!但是我希望这些类级别的变量可以在另一个类中使用。我该怎么做?在另一个类中使用变量意味着你想在另一个类中使用值??是的..实际上我想通过使这个类成为一个简单的java类来创建这个类的对象,并希望在另一个类中使用这些变量。你不能通过创建对象来获得值。若您创建对象,那个么该变量将获得另一个实例,这样您就不能有值。您需要将此值存储在共享首选项之类的位置,并将其保存在另一个class.srry bro中。但我不知道。如果你能实施,那么一定要通知我。这对我也有帮助。