1

我有两个类MainActivity.java (Main Class) & MyCurrentLoctionListener.java

当我运行我的应用程序 (onLoad-onCreate) 时,我愿意获取我当前的地理位置:

我得到你好 :: null

即使我将 myLocation 定义为 public ,所以 MainActivity.java (主类)可以看到它。

MainActivity.java :

package com.example.test;

import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn1 = (Button) findViewById(R.id.make_folder);
TextView txt1 = (TextView) findViewById(R.id.textView1);

//import android.location.LocationManager;
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyCurrentLoctionListener locationListener = new MyCurrentLoctionListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);    

txt1.setText("HI :: " + locationListener.myLocation);

btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {   
}           
}); //setOnClickListener    
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

MyCurrentLoctionListener.java :

package com.example.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class MyCurrentLoctionListener implements LocationListener {

public String myLocation;

@Override
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();



myLocation = "Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude();

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
}

权限已包含在 Manifest.xml 中

4

2 回答 2

3

当您开始活动时,您的 MyCurrentLoctionListener 将无法立即获取位置。

最好将 TextView 传递给 MyCurrentLoctionListener 类,如下所示:

public class MyCurrentLoctionListener implements LocationListener {

public String myLocation;
private TextView mTextView;

MyCurrentLoctionListener(TextView tv) {
    this.mTextView = tv;    
}

@Override
public void onLocationChanged(Location location) {
    location.getLatitude();
    location.getLongitude();

    mTextView.setText("Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude());

}

....
于 2013-10-29T17:09:48.340 回答
1

这可能是因为您过早使用 myLocation。当您使用 onLocationChanged 请求 GPS 数据时,可能需要一秒钟才能找到您的坐标,因此您需要考虑到这一点。也许在 onLocationChanged 函数中更改 TextView 或类似的东西。

于 2013-10-29T17:06:45.927 回答