多线程时无法使用setTimeout()

new java.lang.Thread(function() {

}).start();
多线程里使用setTimeout()会崩溃

Version: 221
Android: 23
org.mozilla.javascript.WrappedException: Wrapped java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() (<<remote>:Untitled-1>#15)
at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1914)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:148)
at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
at org.mozilla.javascript.BoundFunction.call(BoundFunction.java:53)
at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1479)
at script(<<remote>:Untitled-1>:15)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:815)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:399)
at com.stardust.autojs.engine.RhinoJavaScriptEngine$InterruptibleAndroidContextFactory.doTopCall(RhinoJavaScriptEngine.java:210)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3452)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:107)
at org.mozilla.javascript.InterfaceAdapter.invokeImpl(InterfaceAdapter.java:137)
at org.mozilla.javascript.InterfaceAdapter$1.run(InterfaceAdapter.java:83)
at org.mozilla.javascript.Context.call(Context.java:526)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:509)
at org.mozilla.javascript.InterfaceAdapter.invoke(InterfaceAdapter.java:86)
at org.mozilla.javascript.jdk13.VMBridge_jdk13$1.invoke(VMBridge_jdk13.java:132)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy8.run(Unknown Source)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at com.stardust.autojs.runtime.api.Timers.ensureHandler(Timers.java:28)
at com.stardust.autojs.runtime.api.Timers.setTimeout(Timers.java:33)
at java.lang.reflect.Method.invoke(Native Method)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
... 18 more

开发者

setTimeout本身就是单线程的产物。在自己启用的新线程中没有Looper是不能使用setTimeout的。

  • 2
    帖子
  • 546
    浏览

与 Auto.js 的连接断开,我们正在尝试重连,请耐心等待