使用 sendbroadcast 获取 GPS 位置时出现 NullPointerException

Get NullPointerException when use sendbroadcast for GPS location

通过 Whosebug 进行研究后没有找到答案,我决定 post 这个问题。

我正在尝试获取我的 GPS 位置并将其广播到我的 MainActivity。 在 MainActivity 中我有这个:

  //get Your Current Location
    LocationManager locationManager=    (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    MyCurrentLocationListener locationListener = new MyCurrentLocationListener();

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) locationListener);

MyCurrentLocationListener 看起来像这样:

public class MyCurrentLocationListener extends Activity implements LocationListener {



@Override
public void onLocationChanged(Location location) {



    location.getLatitude();
    location.getLongitude();


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

    //I make a log to see the results
    Log.e("MY CURRENT LOCATION", myLocation);






    Intent intent_lat=new Intent("loc_lat");
    intent_lat.putExtra("latitud",location.getLatitude());
    sendBroadcast(intent_lat);

}

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

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}

}

我一直在尝试很多事情,比如将双纬度转换为字符串,使我的 class 不从 activity 延伸(但那样我就不能使用 sendBroadcast)。

我该怎么办?如果您需要更多信息,请告诉我。

谢谢。

编辑:logcat

09-30 12:58:47.513 32019-32019/com.smartglove.uma.smartgloveuma    E/MY CURRENT LOCATION: Latitude = 37.18993917 Longitude = -3.60485555
09-30 12:58:47.515 32019-32019/com.smartglove.uma.smartgloveuma D/AndroidRuntime: Shutting down VM
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime: FATAL EXCEPTION: main
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime: Process: com.smartglove.uma.smartgloveuma, PID: 32019
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:377)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at com.smartglove.uma.smartgloveuma.MyCurrentLocationListener.onLocationChanged(MyCurrentLocationListener.java:37)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at android.location.LocationManager$ListenerTransport.access[=12=]0(LocationManager.java:210)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at android.location.LocationManager$ListenerTransport.handleMessage(LocationManager.java:226)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

您需要在 MainActivity 中实现 LocationListener 接口并将您的代码更改为:

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);