【发布时间】:2015-09-30 10:49:44
【问题描述】:
在通过 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) {
}
}
我一直在尝试很多事情,例如将双纬度转换为字符串,使我的课程不从活动扩展(但那样我不能使用 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)
【问题讨论】:
-
为什么不在
MainActivity本身中实现LocationListener。MyCurrentLocationListener这个类扩展了Activity,这是为什么呢? -
显示来自
logcat的错误日志 -
一开始它并没有扩展Activity,但我不得不这样做才能使用sendBroadcast(因为我不能使用它,甚至指定上下文)
-
仅使用
Activity扩展您的课程不会让您获得Context。您只能在其任何生命周期方法中获取它。例如onCreate() -
但是我的类 MyCurrentLocationListener 没有任何 onCreate 方法,我应该实现它吗?抱歉,我是 Android 开发新手
标签: android nullpointerexception gps location broadcast