Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 关闭/打开位置传感器后,位置管理器在OnFormShow上崩溃_Android_Delphi_Location_Delphi 10.1 Berlin_Android 5.1.1 Lollipop - Fatal编程技术网

Android 关闭/打开位置传感器后,位置管理器在OnFormShow上崩溃

Android 关闭/打开位置传感器后,位置管理器在OnFormShow上崩溃,android,delphi,location,delphi-10.1-berlin,android-5.1.1-lollipop,Android,Delphi,Location,Delphi 10.1 Berlin,Android 5.1.1 Lollipop,我使用的是本机位置管理器,但如果我关闭位置传感器,然后再次打开,应用程序会在启动后立即崩溃(位置传感器图标会闪烁)。然而,如果我模仿一个新的位置,那么它不会崩溃,工作得很完美 另外,当应用程序在启动时不断崩溃时,如果我运行Waze,等待10秒,然后再次启动,它就会工作 unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FM

我使用的是本机位置管理器,但如果我关闭位置传感器,然后再次打开,应用程序会在启动后立即崩溃(位置传感器图标会闪烁)。然而,如果我模仿一个新的位置,那么它不会崩溃,工作得很完美

另外,当应用程序在启动时不断崩溃时,如果我运行Waze,等待10秒,然后再次启动,它就会工作

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  Androidapi.JNI.Location, Androidapi.JNIBridge, Androidapi.JNI.JavaTypes,
  Androidapi.JNI.Os, FMX.Layouts, FMX.ListBox, FMX.StdCtrls,
  FMX.Controls.Presentation, Androidapi.Helpers;

type

  TLocationListener = class;

  TForm1 = class(TForm)
    Label1: TLabel;
    procedure FormShow(Sender: TObject);
  private
    FLocationManager : JLocationManager;
    locationListener : TLocationListener;
  public
    destructor Destroy; override;
    procedure onLocationChanged(location: JLocation);
  end;

  TLocationListener = class(TJavaLocal, JLocationListener)
  private
    [weak]
    FParent : TForm1;
  public
    constructor Create(AParent : TForm1);
    procedure onLocationChanged(location: JLocation); cdecl;
    procedure onProviderDisabled(provider: JString); cdecl;
    procedure onProviderEnabled(provider: JString); cdecl;
    procedure onStatusChanged(provider: JString; status: Integer; extras: JBundle); cdecl;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText;

{ TLocationListener }

constructor TLocationListener.Create(AParent: TForm1);
begin
  inherited Create;
  FParent := AParent;
end;

procedure TLocationListener.onLocationChanged(location: JLocation);
begin
  FParent.onLocationChanged(location);
end;

procedure TLocationListener.onProviderDisabled(provider: JString);
begin

end;

procedure TLocationListener.onProviderEnabled(provider: JString);
begin

end;

procedure TLocationListener.onStatusChanged(provider: JString; status: Integer;
  extras: JBundle);
begin

end;

{ TForm1 }

destructor TForm1.Destroy;
begin
  if Assigned(locationListener) then
    FLocationManager.removeUpdates(locationListener);
  inherited;
end;

procedure TForm1.FormShow(Sender: TObject);
var
  LocationManagerService: JObject;
  location : JLocation;
begin
  if not Assigned(FLocationManager) then
  begin
    LocationManagerService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
    FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
    if not Assigned(locationListener) then
      locationListener := TLocationListener.Create(self);
    FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 2000, 20, locationListener,TJLooper.JavaClass.getMainLooper);
  end;
  location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
  onLocationChanged(location);
end;

procedure TForm1.onLocationChanged(location: JLocation);
begin
 Label1.Text := FloatToStr((location.getSpeed*3600)/1000) + ' km/h';
end;

end.

我错过了什么?事件的哪一部分可能会导致此崩溃?

如果没有确切说明崩溃的位置和/或调用堆栈,很难说它是在启动屏幕后的黑屏期间崩溃的,位置图标只是闪烁,应用程序关闭。我通过删除这两行来解决这个问题:location:=LocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS\u PROVIDER);onLocationChanged(位置);但是,直到手机从实际位置更改位置,它才会触发位置更改。您是否使用调试器来确定它是否确实在您删除的线路上崩溃?实际上,我无法使用调试器,因为我的手机不再通过USB与PC通信,也没有SSD空间。我必须通过Wi-Fi将每个.apk复制到手机上:(我通过移动这两行
location:=FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);onLocationChanged(location);
OnFormPaint
并设置一个布尔变量,使其只发生一次(在应用程序启动后)。抱歉,我最近问了太多问题。如果没有确切说明崩溃的位置和/或调用堆栈,很难说它在启动屏幕后的黑屏期间崩溃了,位置图标只是闪烁,应用程序关闭。我通过删除以下两行来解决问题:位置:=位置管理器.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);onLocationChanged(location);但是,它不会触发LocationChange,直到手机从实际位置更改其位置。您是否使用调试器确定它是否确实在线路上崩溃你删除了?实际上我无法使用调试器,因为我的手机不再通过USB与PC通信,也没有SSD空间。我必须通过Wi-Fi将每个.apk复制到手机:(我通过移动这两行来解决问题
location:=FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);onLocationChanged(位置);
OnFormPaint
并设置一个布尔变量,使其只发生一次(在应用程序启动后)。对不起,我最近问的问题太多了。