Recent

Author Topic: Need help with crasch after reading GeomagneticField  (Read 1355 times)

stab

  • Full Member
  • ***
  • Posts: 182
Need help with crasch after reading GeomagneticField
« on: August 10, 2013, 06:30:57 pm »
Hi all,

Can someone explain why my app crashes after reading declination from GeomagneticField.
(to be used to adjust azimuth from magnetic field reading )

My code:
Code: [Select]
procedure TfrmJNITestMain.btnGeoMagneticFieldClick(Sender: TObject);
var
  geoMagneticFieldClass : jclass;
  javaMethodId_GeoMagneticField, javaMethodId_GetDeclination : JMethodID;
  geoMagneticFieldObject : JOBject;
  lParams: array[0..3] of JValue;
  gdLatitudeDeg, gdLongitudeDeg, altitudeMeters : jfloat;
  timeMillis : longint;
  millis :int64;
  declination : double;
begin
  // get geoMagneticFieldClass
  geoMagneticFieldClass := javaEnvRef^^.FindClass(javaEnvRef, 'android/hardware/GeomagneticField');
  if geoMagneticFieldClass = nil then
    debugln('******** geoMagneticFieldClass NIL ********')
  else
    debugln('******** geoMagneticFieldClass NOT NIL ********');

  javaMethodId_GeoMagneticField := javaEnvRef^^.GetMethodID(javaEnvRef, geoMagneticFieldClass, '<init>', '(FFFJ)V');
  if javaMethodId_GeoMagneticField = nil then
    debugln('******** javaMethodId_GeoMagneticField NIL ********')
  else
    debugln('******** javaMethodId_GeoMagneticField NOT NIL ********');

//  GeomagneticField (float gdLatitudeDeg, float gdLongitudeDeg, float altitudeMeters, long timeMillis)
  // Get the geoMagneticFieldObject
  gdLatitudeDeg := 0;
  gdLongitudeDeg := 0;
  altitudeMeters := 0;
  millis := MilliSecondsBetween(Now, StrToDateTime('1-1-1970'));
  timeMillis := millis;

  lParams[0].f := gdLatitudeDeg;
  lParams[1].f := gdLongitudeDeg;
  lParams[2].f := altitudeMeters;
  lParams[3].j := timeMillis;
  if javaMethodId_GeoMagneticField <> nil then
  begin
    geoMagneticFieldObject := javaEnvRef^^.CallObjectMethodA(javaEnvRef, javaActivityObject, javaMethodId_GeoMagneticField, @lParams[0]);
    if geoMagneticFieldObject = nil then
      debugln('******** geoMagneticFieldObject NIL ********')
    else
      debugln('******** geoMagneticFieldObject NOT NIL ********');
  end;

  if javaMethodId_GeoMagneticField <> nil then
  begin
    // Get method id for method: getDeclination
    javaMethodId_GetDeclination := javaEnvRef^^.GetMethodID(javaEnvRef, geoMagneticFieldClass, 'getDeclination', '()F');
    if javaMethodId_GetDeclination = nil then
      debugln('******** javaMethodId_GetDeclination NIL ********')
    else
      debugln('******** javaMethodId_GetDeclination NOT NIL ********');

    // Now call getDeclination method in the geoMagneticFieldObject
    declination := javaEnvRef^^.CallFloatMethod(javaEnvRef, geoMagneticFieldObject, javaMethodId_GetDeclination);
    debugln('******** jdeclination: ' + FloatToStrF(declination, ffFixed, 15, 3) + ' ********');
  end;
end;

The app returns the declination OK but then crashes.

What am I doing wrong? %)

Ane insight appreciated

Regards
stab