Can a thread trigger view changes?

30 views
Skip to first unread message

Arne Mailand

unread,
Oct 22, 2016, 4:31:34 PM10/22/16
to App Inventor Open Source Development
Hey guys,

is there any way to let a thread trigger an event which would do changes on the UI thread?

for instance, I made a thread which listens for incoming data on a socket. This thread is supposed to call a method on the UI-Thread which is called "OnDataIn". This method is defined as @SimpleEvent. In AI, I can then assign some view changes, like changing some font color, as this event occurs (when .. .OnDataIn > do "set ... font color to read").

During runtime, when i click a button to start that thread, i get this message:

Runtime Error
Only the original thread that created a view hierarchy can touch its views.
Note: You will not see another error reported for 5 seconds.

Well... does this mean that everything which happens inside the event handler  "when .. .OnDataIn" is actually happening the thread calling this and not on the UI thread?
Is there any way to solve this? 
The only way I thought of is to make the thread write the incoming data into an invisible data field and some methods from the UI thread keeps reading that data periodically to see if there are any changes ("polling") but I think this is not the way it is supposed to be and difficult to realize. I.e., I would have to make a infinite loop with a X milliseconds delay which would read the data between the delays, but this would block the whole UI-Thread and make the app useless. :-(

Arne Mailand

unread,
Oct 22, 2016, 4:39:42 PM10/22/16
to App Inventor Open Source Development
Oh guys, I think I overread the last part of the instructions regarding threads.

I think I have to call the method ("OnDataIn") like this, right?

activity.runOnUiThread(new Runnable() {

       @Override
       public void run() {
         OnDataIn();

} }


I'm on it...

Arne Mailand

unread,
Oct 22, 2016, 5:19:51 PM10/22/16
to App Inventor Open Source Development

I get this error on compiling:


error: cannot find symbol

    [javac]         activity.runOnUiThread(new Runnable() {

    [javac]         ^



When I type "Activity" in high capital letters, I get this error:


error: non-static method runOnUiThread(Runnable) cannot be referenced from a static context


hmm any ideas?

Arne Mailand

unread,
Oct 22, 2016, 6:18:52 PM10/22/16
to App Inventor Open Source Development
well, at least I succeeded in compilating.

I had to declare

private final Activity activity;

and initialize activity in the constructor:

        activity = container.$context();

Anyways, none of the functions I put into the event-handler are working. I would be very thankful for any advices.

Here are the methods btw.:


    @SimpleFunction(description = "test.")
public void XThread(final int threadID){

AsynchUtil.runAsynchronously(new Runnable() {@Override
        public void run() {

activity.runOnUiThread(new Runnable() {
@Override
public void run() {

                        ThreadFinishUI(threadID);

} });
}});

}

@SimpleEvent(description = "Event-Handler Thread")
public void ThreadFinishUI(int threadID){

EventDispatcher.dispatchEvent(this, "ThreadFinish", threadID);

}

Arne Mailand

unread,
Oct 22, 2016, 11:25:42 PM10/22/16
to App Inventor Open Source Development
Guys, I found my mistake (second parameter in the event dispatcher ;-) )
Reply all
Reply to author
Forward
0 new messages