在Android中创建实例表单时,delphi中的TTimer会导致错误的线程异常

时间:2016-04-22 03:39:15

标签: delphi firemonkey delphi-xe8

我的问题是我有一个计时器来及时显示表格。在iOS中,它可以很好地与我的代码配合使用,但在android中,它会显示异常:.map_canvas{ position:absolute; }。但我认为delphi上的计时器将在主线程下运行?我还要android.view.viewrootimpl$calledfromwrongthreadexception only the original thread that created a view hierarchy can touch its views进行检查,但它仍然是同一个例外。知道如何处理这个问题。感谢。

这是我的代码。我用Form1启动应用程序,里面有计时器:

TThread.Synchronize

在Form1中的DoScreenTimerEvent函数中将显示Form2(这是我的应用程序中的屏幕保护程序):

timer := TTimer.create(nil);
timer.Interval := time
timer.OnTimer := DoScreenTimerEvent;

问题是我有很多形式,如Form3,Form4等... Form2将在我推入计时器的时间间隔后显示,每当我在Form3中,或者4 ...在iOS中,它完美的工作,但在Android我有上述例外。

1 个答案:

答案 0 :(得分:0)

如异常所示,您无法修改UI线程之外的视图。如果您在后台线程上并且需要处理视图,请执行以下操作:

mActivity.runOnUiThread(new Runnable() {
    public void run() {
        // your code here
    }
});

或者,您可以

new Handler(Looper.getMainLooper()).post(new Runnable() {
    public void run() {
        // your code here
    }
});