当一个类型为Location的变量传递给" onLocationChanged"。应用程序崩溃

时间:2014-05-17 21:04:24

标签: java android locationlistener

如下所示,为什么当我将从mCurrentLocation检索到的getLastKnownLocation传递给onLocationChanged回调时,应用程序会崩溃?

JavaCode:

private void prepareGPS() {
    // TODO Auto-generated method stub
    this.mCriteria = new Criteria();
    setLocationProviderCriteria(this.mCriteria);
    this.mProvider = mLocMgr.getBestProvider(getLocationProviderCriteria(), false);
    if (this.mProvider == null) {
        Toast.makeText(getApplicationContext(), NO_PROVIDER, Toast.LENGTH_LONG).show();
    }else {
        this.mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
        this.mCurrentLocation = this.mLocMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        this.onLocationChanged(mCurrentLocation);
    }
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    if (location != null) {
        this.latEt.setText(String.valueOf(location.getLatitude()));
        this.lngEt.setText(String.valueOf(location.getLongitude()));
    }else {
        Toast.makeText(getApplicationContext(), "onLocationChanged returned NULL", Toast.LENGTH_LONG).show();
    }
}

logcat的:

05-18 00:04:02.190: E/AndroidRuntime(9311): FATAL EXCEPTION: main
05-18 00:04:02.190: E/AndroidRuntime(9311): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.meetingpointlocator_03/com.example.meetingpointlocator_03.MeetingPointFix}: java.lang.NullPointerException
05-18 00:04:02.190: E/AndroidRuntime(9311):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at android.app.ActivityThread.access$700(ActivityThread.java:168)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at android.os.Looper.loop(Looper.java:137)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at android.app.ActivityThread.main(ActivityThread.java:5493)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at java.lang.reflect.Method.invokeNative(Native Method)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at java.lang.reflect.Method.invoke(Method.java:525)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at dalvik.system.NativeStart.main(Native Method)
05-18 00:04:02.190: E/AndroidRuntime(9311): Caused by: java.lang.NullPointerException
05-18 00:04:02.190: E/AndroidRuntime(9311):     at com.example.meetingpointlocator_03.MeetingPointFix.onLocationChanged(MeetingPointFix.java:163)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at com.example.meetingpointlocator_03.MeetingPointFix.prepareGPS(MeetingPointFix.java:155)
05-18 00:04:02.190: E/AndroidRuntime(9311):     at com.example.meetingpointlocator_03.MeetingPointFix.onCreate(MeetingPointFix.java:60)

2 个答案:

答案 0 :(得分:0)

onLocationChanged()是一个回调,意味着它是事件驱动的,不应该显式调用。操作系统在获得定位后调用它。呼吁......的所有方法都是这样的,即。回调。您必须等待获取位置修复并假设位置将为null,直到调用回调。

答案 1 :(得分:0)

尝试在注册听众this.onLocationChanged(mCurrentLocation);之前致电requestLocationUpdates。我想,你在注册你的监听器之后就打断了回调。另外,请看看explanatory graph,我想它可能会根据优先级来了解这些回调是如何工作的。

this.onLocationChanged(mCurrentLocation);
this.mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
this.mCurrentLocation = this.mLocMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);