As shown below, why when i pass the mCurrentLocation
which is retrieved from getLastKnownLocation
to onLocationChanged
callback, the app crashes?
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)
Try to call this.onLocationChanged(mCurrentLocation);
before registering your listener requestLocationUpdates
. I think, you are interrupting the callback after registering your listener.Also, please have a look on the explanatory graph, i think it might give an idea how these callbacks are working according to the precedences.
this.onLocationChanged(mCurrentLocation);
this.mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
this.mCurrentLocation = this.mLocMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);