android - android 中的位置监听器给出 java.lang.IllegalArgumentException : listener==null

标签 android gps locationlistener

嘿,当我运行应用程序时,它给出了一个错误 java.lang.IllegalArgumentException: listener==null ,它告诉 listener 为 null。

我的示例代码在这里:

public class HelloAndroidGpsActivity extends Activity {
private EditText editTextShowLocation;
private Button buttonGetLocation;
private LocationManager locManager;
private LocationListener locListener;
private Location mobileLocation;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editTextShowLocation = (EditText) findViewById(R.id.editTextShowLocation);
    buttonGetLocation = (Button) findViewById(R.id.buttonGetLocation);  
    buttonGetLocation.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            buttonGetLocationClick();
        }
    });
}

/** Gets the current location and update the mobileLocation variable*/
private void getCurrentLocation() {
    locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    System.out.println("mobile location manager is ="+locManager);
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
    locListener = new LocationListener() {  
        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            System.out.println("mobile location is in listener1");
        }
        @Override
        public void onProviderEnabled(String provider) {
            System.out.println("mobile location is in listener2");
        }
        @Override
        public void onProviderDisabled(String provider) {
            System.out.println("mobile location is in listener3");
        }
        @Override
        public void onLocationChanged(Location location) {
            System.out.println("mobile location is in listener="+location);
            mobileLocation = location;
        }
    };
    System.out.println("after setting listener");
}
private void buttonGetLocationClick() {
    getCurrentLocation(); 
    System.out.println("mobile location is ="+mobileLocation);  
    if (mobileLocation != null) {
        locManager.removeUpdates(locListener); 
        String londitude = "Londitude: " + mobileLocation.getLongitude();
        String latitude = "Latitude: " + mobileLocation.getLatitude();
        String altitiude = "Altitiude: " + mobileLocation.getAltitude();
        String accuracy = "Accuracy: " + mobileLocation.getAccuracy();
        String time = "Time: " + mobileLocation.getTime();
        editTextShowLocation.setText(londitude + "\n" + latitude + "\n"
                + altitiude + "\n" + accuracy + "\n" + time);
    } else {
        editTextShowLocation.setText("Sorry, location is not determined");
    }
}
}

文本框中的输出是“抱歉,位置未确定”” 如果有人能告诉我问题出在哪里,请帮助我。 谢谢

最佳答案

在使用之前初始化监听器。

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

此时locListener为null,你在这行代码之后初始化它。这可能是原因。

所以像这样重新排列你的代码行;

   locListener = new LocationListener() {...};
   locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

希望这可能有助于解决您的问题...

关于android - android 中的位置监听器给出 java.lang.IllegalArgumentException : listener==null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11863424/

相关文章:

java - 为什么我的 .isEmpty 在此代码中不起作用?

android - 使用 SAX 解析递归 XML

python - 解析和转换 GPS Dongle 的输出

iphone - iOS: locationManager:didUpdateToLocation:fromLocation: 不移动时报告速度

android - 如何获取android安装的应用程序的路径

php - 使用 MySQL SELECT AS 获取当前纬度/经度

android - LocationClient - 模拟位置

android - 如何计算从当前位置到其他位置的总行驶里程位置监听器?

java - 使用 LocationListener 的带 GPS 的运动跟踪器

android - 带缓存的图像加载器?