使用sendbroadcast进行GPS定位时获取NullPointerException

时间:2015-09-30 10:49:45

标签: android nullpointerexception gps location broadcast

在通过StackOverflow进行研究而没有找到答案之后,我决定发布这个问题。

我正在尝试获取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) {

}

}

我一直在尝试很多东西,例如将双倍纬度转换为String,使我的类不会从活动中扩展(但这样我就不能使用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$000(LocationManager.java:210)
09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma E/AndroidRuntime:     at android.location.LocationManager$ListenerTransport$1.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)

1 个答案:

答案 0 :(得分:0)

您需要在MainActivity中实施LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 界面,并将代码更改为:

unsafe