Unity3D上的Java插件无法启动新线程

Unity3D上的Java插件无法启动新线程,java,android,multithreading,unity3d,Java,Android,Multithreading,Unity3d,我有一个java插件,用于我开发的简单java应用程序。 这个插件启动了一个新的线程,也使用网络 执行的类正在实现Runnable并以以下方式启动线程: AsyncTask.execute(this); 这对java应用程序很好,运行起来没有任何问题,但是,当我尝试用Unity3D运行这个插件时,它会抛出一个错误,并且不会运行我的插件 在Unity3D中执行的代码: var token="AbCd123456789"; var jo = new AndroidJavaObject("com.e

我有一个java插件,用于我开发的简单java应用程序。 这个插件启动了一个新的线程,也使用网络

执行的类正在实现Runnable并以以下方式启动线程:

AsyncTask.execute(this);
这对java应用程序很好,运行起来没有任何问题,但是,当我尝试用Unity3D运行这个插件时,它会抛出一个错误,并且不会运行我的插件

在Unity3D中执行的代码:

var token="AbCd123456789";
var jo = new AndroidJavaObject("com.edealya.lib.AppayableUnityAdapter");
jo.Call("runApplayble",token);
可运行的方法是:

public void runAppayable(String token){
DeviceIdentifier edDevice = new DeviceIdentifier(
                                this.getApplicationContext(),token);
edDevice.update();
}

和edDevice.update();是启动新线程的方法

I/Unity   (27624): Exception: java.lang.RuntimeException: Can't create handler inside     thread that has not called Looper.prepare()
I/Unity   (27624):   at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in   <filename unknown>:0 
I/Unity   (27624):   at UnityEngine.AndroidJNISafe.NewObject (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0 
I/Unity   (27624):   at UnityEngine.AndroidJavaObject._AndroidJavaObject (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0 
I/Unity   (27624):   at UnityEngine.AndroidJavaObject..ctor (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0 
I/Unity   (27624):   at ReflectionFx.startAppayable () [0x00006] in /Users/Shared/Unity/4-0_AngryBots/Assets/Scripts/Fx/ReflectionFx.cs:30 
I/Unity   (27624):   at ReflectionFx.Start () [0x00000] in /Users/Shared/Unity/4-0_AngryBots/Assets/Scripts/Fx/ReflectionFx.cs:38 
I/Unity(27624):异常:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序
I/Unity(27624):位于:0中的UnityEngine.AndroidJNISafe.CheckException()[0x00000]
I/Unity(27624):位于UnityEngine.AndroidJNISafe.NewObject(IntPtr clazz、IntPtr methodID、UnityEngine.jvalue[]args)[0x00000]中:0
I/Unity(27624):位于UnityEngine.AndroidJavaObject.\u AndroidJavaObject(System.String类名,System.Object[]args)[0x00000]in:0
I/Unity(27624):位于UnityEngine.AndroidJavaObject..ctor(System.String类名,System.Object[]args)[0x00000]中:0
I/Unity(27624):位于/Users/Shared/Unity/4-0_AngryBots/Assets/Scripts/Fx/ReflectionFx.cs:30中的ReflectionFx.startapayable()[0x00006]
I/Unity(27624):位于/Users/Shared/Unity/4-0_AngryBots/Assets/Scripts/Fx/ReflectionFx.cs:38中的ReflectionFx.Start()[0x00000]

如何解决此问题?

异步任务必须在UiThread或具有Looper的线程上启动,请尝试使用

myActivity.runOnUiThread(new Runnable(){AsyncTask.execute(this);})

或者在启动它之前在线程上调用Looper.prepare()

只需读取stacktrace,在我看来,Unity层正在捕获运行时异常并转储一些不同的内容

我看到了一个类似的带有NullPointerException的stacktrace:我看到的不是追溯到代码中问题所在位置的堆栈跟踪,而是Unity JNI stacktrace


有人能解释为什么会这样,以及如何让Unity转储正确的stacktrace吗?

当您使用Lopper.prepare()时,是否有任何不同的日志;AsyncTask.execute(this);?我删除了线程,但它仍然抛出相同的错误!真奇怪!尝试添加更多的代码,这样任何人都可以发现问题