Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GPS定位监听器_Android_Gps - Fatal编程技术网

Android GPS定位监听器

Android GPS定位监听器,android,gps,Android,Gps,我希望只获得一次以下值。如何阻止听众?我获取这些值,然后在onLocationChanged()中removeUpdates()方法给我一个空指针异常。所有这些都是通过单击按钮调用的 public void onLocationChanged(Location loc) { locationManager.removeUpdates(this); //editLocation.setText(""); /

我希望只获得一次以下值。如何阻止听众?我获取这些值,然后在
onLocationChanged()
removeUpdates()
方法给我一个空指针异常。所有这些都是通过单击按钮调用的

 public void onLocationChanged(Location loc) {
             locationManager.removeUpdates(this);
                //editLocation.setText("");
                //pb.setVisibility(View.INVISIBLE);
                Toast.makeText(
                        getBaseContext(),
                        "Location changed: Lat: " + loc.getLatitude() + " Lng: "
                                + loc.getLongitude(), Toast.LENGTH_SHORT).show();
                String longitude = "Longitude: " + loc.getLongitude();
                //Log.v(TAG, longitude);
                String latitude = "Latitude: " + loc.getLatitude();
                //Log.v(TAG, latitude);

            /*------- To get city name from coordinates -------- */
                String cityName = null;
                String StreetName = null;
                Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
                List<Address> addresses;
                            try {
                                locationManager=null;
                    addresses = gcd.getFromLocation(loc.getLatitude(),
                            loc.getLongitude(), 1);
                    if (addresses.size() > 0)
                        System.out.println(addresses.get(0).getLocality());
                    cityName = addresses.get(0).getLocality();
                    if (addresses.size() > 0)
                        System.out.println(addresses.get(0).getThoroughfare());
                    StreetName=addresses.get(0).getThoroughfare();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
                String s = longitude + "\n" + latitude + "\n\nMy Current City is: "
                        + cityName;
                //editLocation.setText(s);
                Toast.makeText(
                        getBaseContext(),
                        "Location: " + s, Toast.LENGTH_SHORT).show();
                Toast.makeText(
                        getBaseContext(),
                        "Street Name: " + StreetName, Toast.LENGTH_LONG).show();
                //locationManager.removeUpdates(locationListener);

            }
在重新创建之前:

private LocationManager locationManager;
private LocationListener locationListener;

我明白问题所在。您正在
Punchout()
方法中创建局部变量,而不是使用活动中定义的实例变量

只要确保始终使用实例变量,就可以在
onLocationChanged()


显示您在何处声明和初始化
locationManager
。只需编辑以反映
private LocationManager locationManager;
private LocationListener locationListener;
public void Punchout(View view) {
    boolean hasNetwrokProvider;
    Toast.makeText(getApplicationContext(), "Getting Location", Toast.LENGTH_LONG).show();

  /* These lines created local variables:
  LocationManager locationManager = (LocationManager)
          getSystemService(Context.LOCATION_SERVICE);
  LocationListener locationListener = new MyLocationListener();
  */

  //use the instance variables instead:
  locationManager = (LocationManager)
          getSystemService(Context.LOCATION_SERVICE);
  locationListener = new MyLocationListener();

  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 3, locationListener);
}