@SimpleFunction(description = "Returns the sum of the given list of integers.")
public void SumAll(YailList integers) {
int sum = 0;
for (final Object o : integers.toArray()) {
try {
sum += Integer.parseInt(o.toString());
} catch (NumberFormatException e) {
throw new YailRuntimeError(e.toString(), "NumberFormatException");
}
}
SumReturn(sum);
}
@SimpleEvent(description = "This event fires after the sum is calculated and returns the sum")
public void SumReturn(int sum) {
EventDispatcher.dispatchEvent(this, "SumReturn", sum);
}
All events must run on the UI thread. Since you're spawning a background thread to do your computing, you shouldn't be calling the event method from the background thread. Instead, you would do something like:
...
public void run() {
final result = ...; // compute some thing
form.runOnUiThread(new Runnable() {
public void run() {
gotValues(result);
}
});
}
...
Also, the default implementation of start should be sufficient. Don't override it unless you have a good reason, and typically you should call super.
First, the names of the classes and methods in your code are quite confusing. You are going above and above than what is required. You are making it complicated than it is. It looks like you are dealing with decompiled code of progaurded class.
On the startThread() method, you have assigned value of thread object of Test. You should use start() method of Thread class to begin execution of code in run method of Runnable.
In getValues() method, you are using startThread() method for object of type Thread. This method doesn't exist in Thread class.
In gotValues method, you are using EventDispatcher.dispatchEvent() which needs three params, first Component, second event name and third is array of arguments. You should have done something like this,
public class Test extends AndroidNonVisibleComponent {
public Test(ComponentContainer container) {
super(container.$form());
}
@SimpleFunction
public void SumInNewThread(int a, int b) {
//
new Thread(new Runnable() {
@Override
public void run() {
final int sum = a + b;
// Now i have sum of these number, since i cant dispatch event on background thread, i will use runOnUiThread method from Activity class
form.runOnUiThread(new Runnable() {
@Override
public void run() {
GotSum(sum);
}
});
}
}).start();
}
@SimpleEvent
public void GotSum(int sum) {
EventDispatcher.dispatchEvent(this, "GotSum", sum);
}
}
If you are not using java -8 supported tools like Rush, you should declare final when assigning value to v1, v2, v3 and v4 so that it can be acessed from run method like,
final int v1=1,v2=2,v3=3,v4=4;
And you again have not used start method. See example I have provided above.