我刚开始开发一个移动应用程序,它有两个表单,FRM_Main (主表单)和FRM_Party (Party )。通过单击图像,我们可以从FRM_Party打开FRM_Main。图像代码是:
procedure TFRM_Main.IMAGE_PartyClick(Sender: TObject);
begin
FRM_Party := TFRM_Party.Create(Application);
FRM_Party.Show;
end;现在,当FRM_Party调用OnActivate事件时,我将在TMSFMXTableView中加载一些数据。该守则是:
procedure TFRM_Party.FormActivate(Sender: TObject);
var
TableView : TTMSFMXTableViewItem;
I : Integer;
begin
if Class_My_Pro_and_func.Func_DataBaseConnection then // Checks wethere database connection is active or not if not then it connect with database and returns bool value.
Begin
UniQuery.Close;
UniQuery.SQL.Clear;
UniQuery.SQL.Text := 'select * from db_stock.tbl_party where Reg_ID = :Reg_ID and Party_Delete <> :Party_Delete order by Party_Name ';
UniQuery.ParamByName('Reg_ID').AsInteger := d_Glob_Reg_ID;
UniQuery.ParamByName('Party_Delete').AsString := 'F';
UniQuery.Open;
TABLEVIEW_Party.BeginUpdate;
if UniQuery.RecordCount > 0 then
begin
for I := 1 to UniQuery.RecordCount do
begin
TableView := TABLEVIEW_Party.Items.Add;
TableView.Caption := UniQuery.Fields[1].AsString;
TableView.Description := UniQuery.Fields[2].AsString + ' = ' + UniQuery.Fields[3].AsString;
TABLEVIEW_Party.EndUpdate;
UniQuery.Next;
end;
end
else
ShowMessage('No recored Found.');
End
end;在FRM_Party上有一个后退按钮,它将用户带到FRM_Main。此按钮代码是:
procedure TFRM_Party.BTN_Party_BackClick(Sender: TObject);
begin
try
UniQuery.Connection.Close; // Closing Query connection
DB_Connection.Disconnect; // Disconnecting database
FreeAndNil(FRM_Party);
Close;
except
on E: Exception do
ShowMessage(E.Message);
end;
end;除了上面的代码之外,FRM_Party上没有写任何其他东西。
现在,问题是,当我在移动上运行应用程序并一次又一次地打开/关闭FRM_Party时,前4次FRM_Party工作非常好,但是在第五次FRM_Party正确打开时,但是当关闭应用程序时挂起。通过关闭和打开应用程序,我尝试了很多次。每次结果都是一样的。
有谁可以帮我?
发布于 2020-07-04 10:07:40
在OnActivate处理程序中,如果BeginUpdate()和TABLEVIEW_Party上的EndUpdate()不是1,则对UniQuery.RecordCount和EndUpdate()的调用是不平衡的。在进入循环之前调用BeginUpdate(),这很好,但随后在循环中调用EndUpdate(),因此调用的次数与调用BeginUpdate()的次数不同。你需要把这些电话调到同样的级别,例如:
procedure TFRM_Party.FormActivate(Sender: TObject);
var
TableView : TTMSFMXTableViewItem;
I : Integer;
begin
if not Class_My_Pro_and_func.Func_DataBaseConnection then
Exit;
UniQuery.Close;
UniQuery.SQL.Text := 'select * from db_stock.tbl_party where Reg_ID = :Reg_ID and Party_Delete <> :Party_Delete order by Party_Name ';
UniQuery.ParamByName('Reg_ID').AsInteger := d_Glob_Reg_ID;
UniQuery.ParamByName('Party_Delete').AsString := 'F';
UniQuery.Open;
if UniQuery.RecordCount > 0 then
begin
TABLEVIEW_Party.BeginUpdate;
try
for I := 1 to UniQuery.RecordCount do
begin
TableView := TABLEVIEW_Party.Items.Add;
TableView.Caption := UniQuery.Fields[1].AsString;
TableView.Description := UniQuery.Fields[2].AsString + ' = ' + UniQuery.Fields[3].AsString;
UniQuery.Next;
end;
finally
TABLEVIEW_Party.EndUpdate;
end;
end
else
ShowMessage('No record Found.');
end;此外,在back按钮的OnClick处理程序中,您在调用FreeAndNil(FRM_Party)之后在FRM_Party上调用FreeAndNil(FRM_Party)。
在RADStudio10.4之前的移动平台上,对象生存期由ARC管理。。调用FreeAndNil(FRM_Party)实际上不会释放FRM_Party对象,它只会将FRM_Party变量设置为nil。对象未被释放,因为Application仍然具有对它的活动引用。
但是在RADStudio10.4之前,弧不再被使用,所以调用FreeAndNil(FRM_Party)实际上会破坏对象。因此,事后调用Close()将导致代码崩溃。
当TForm关闭时,释放它的正确方法是使用它的OnClose事件,例如:
procedure TFRM_Party.FormDestroy(Sender: TObject);
begin
If FRM_Party = Self then
FRM_Party := nil;
end;
procedure TFRM_Party.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := TCloseAction.caFree;
end;
procedure TFRM_Party.BTN_Party_BackClick(Sender: TObject);
begin
UniQuery.Connection.Close;
DB_Connection.Disconnect;
Close;
end;https://stackoverflow.com/questions/62726074
复制相似问题