【发布时间】:2016-01-04 07:20:14
【问题描述】:
我正在使用 Delphi xe8 制作一个测试 android 多设备应用程序。我将对象附加到列表框中的项目,如下所示:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics,
Androidapi.JNI.JavaTypes, FMX.Dialogs,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.ListBox,
Androidapi.Helpers;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
//this where I am Attaching objects to items
procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
jstr1:JString;
begin
str:='apple';
jstr1:=StringToJString(str);
ListBox1.Items.AddObject('fruit', TObject(jstr1));
end;
//this where I am extracting the jstring objects
procedure TForm1.Button2Click(Sender: TObject);
var
jstr2:JString;
str2:string;
begin
jstr2:=JString(ListBox1.Items.Objects[i]);
str2:=JStringToString(jstr2);
showmessage('the fruit of the day is '+str2);
end;
end.
上面的代码运行正常,并且 jstring 对象附加到项目,但是,当我想提取已附加到项目的 jstring 对象时,我这样做:
jstr2:=JString(ListBox1.Items.Objects[i]);
//Above give me an AV: I get incompatible types TObject and JString
str2:=JStringToString(jstr2);
由于类型 TObject 和 JString 不兼容,上述代码无法编译。但是,如果附加了一个字符串作为对象(而不是一个 jstring)并且想要取回那些我可以做的字符串对象:
str2:=String(ListBox1.Items.Objects[i]);
这适用于常规字符串。我该如何解决这个问题,附加和提取jstring?
【问题讨论】:
-
FWIW,AV 是访问冲突,意味着您正在访问不属于程序的内存,在运行时。我猜你的意思是编译期间出现编译器错误?
-
您的错误消息意味着 JString 不是 TObject 后代。您也许可以存储 JString 的地址,但我不熟悉 JString 究竟是什么,所以我不愿意肯定地说。
标签: android delphi listbox android-4.4-kitkat delphi-xe8