0

I'm building an Android app using GPS localization, and for that purpose I need first to display localization parameters like speed, time of last fix, deltatime, distance, an so on. No problem with speed, but as soon as I try to display the getTime(0) result in the textView, the app crashes right after getting the first GPS fix. I don't know how to get the stack view as I'm testing the app directly on my phone, on real GPS signal. I had hard time configuring the journey simulation via GPX data file (the emulator keeps restarting when I'm trying to simulate the GPS signal), so I went directly to work on the real phone.

Here are the key elements for the problem :

public void displayText(Integer input, double t1, double t0, int signal, int counter) {
 tv.setText(String.valueOf(input));
 t1tv.setText("t1 : "+String.valueOf(t1));
 t0tv.setText("t0 : "+String.valueOf(t0));      
 signaltv.setText("Signal : "+signal);
 countertv.setText("Counter : "+counter);       
}

private class MyLocationListener implements LocationListener {
Integer counter = 0;

public void onLocationChanged(Location loc) {   
 if (loc != null) {
  Integer fix=0;
  Double speed = 0.0;

 if (loc.hasSpeed()) {
  speed = loc.getSpeed() * 1.0; 
  fix=2;
  speed = speed * 3.6d;
  displayText(speed.intValue(), loc.getTime(), 0.0, fix, counter);   
//having the "loc.getTime()" sent to the displayText method is making the app crash.                
                }
  else {
     try {
// Here I have some other code, but it's irrelevant for the occasion as not used when the app chrashes.        } 
                catch (NullPointerException e) {
                }
            }                
            counter = (counter + 1) % data_points;
        }
}

Nothing too complicated I assume, but I'm quite new to programming in Java (been working years on Pascal/Delphi programming), and I kindly asking you for any help.

UPDATE : I changed the function to getElapsedRealtimeNanos(), changed min API to 17, swapped to another VM and managed to run the GPS emulation, and still the app crashes, but now at least I have some trace :

02-21 07:57:08.942: W/dalvikvm(1131): threadid=1: thread exiting with uncaught exception (group=0xb1a53ba8)
02-21 07:57:08.982: E/AndroidRuntime(1131): FATAL EXCEPTION: main
02-21 07:57:08.982: E/AndroidRuntime(1131): Process: com.the314.td, PID: 1131
02-21 07:57:08.982: E/AndroidRuntime(1131): java.lang.NullPointerException
02-21 07:57:08.982: E/AndroidRuntime(1131):     at com.the314.td.testdrive$MyLocationListener.onLocationChanged(testdrive.java:308)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at android.os.Looper.loop(Looper.java:136)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at java.lang.reflect.Method.invoke(Method.java:515)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-21 07:57:08.982: E/AndroidRuntime(1131):     at dalvik.system.NativeStart.main(Native Method)
02-21 07:57:17.652: I/Process(1131): Sending signal. PID: 1131 SIG: 9

1 Answers1

0
getTime () 

Return the UTC time of this fix, in milliseconds (long) since January 1, 1970.

Note that the UTC time on a device is not monotonic: it can jump forwards or backwards unpredictably. So always use getElapsedRealtimeNanos() when calculating time deltas.

On the other hand, getTime() is useful for presenting a human readable time to the user, or for carefully comparing location fixes across reboot or across devices.

All locations generated by the LocationManager are guaranteed to have a valid UTC time, however remember that the system time may have changed since the location was generated.

If you are still not sure please post error trace, so that I can help you better.

TheMohanAhuja
  • 1,855
  • 2
  • 19
  • 30