简体   繁体   中英

Java plugin on Unity3D cant start new thread

I have a java plugin that i use on simple java apps that i develop. This plugin starts a new thread that also uses networking.

The class that executes is implementing Runnable and start the thread this way:

AsyncTask.execute(this);

This works fine with java apps and run without any problem, but, when i try to run this plugin with Unity3D, it throws an error and wont run my plugin

the code executed in Unity3D:

var token="AbCd123456789";
var jo = new AndroidJavaObject("com.edealya.lib.AppayableUnityAdapter");
jo.Call("runApplayble",token);

the runAppayable method is:

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

}

and edDevice.update(); is the method that starts the new thread.

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 

how can i solve this?

AsyncTask must be started on UiThread or thread that have Looper, try to start it with

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

or call Looper.prepare() on thread You are before starting it

Just by reading the stacktrace it looks to me like the Unity layer is catching runtime exceptions and dumping out something different.

I've seen a similar looking stacktrace with NullPointerException: instead of the stack trace that traces back to the place in my code where the problem is, I see this Unity JNI stacktrace.

Can anyone explain why that is and how to get Unity to dump out the proper stacktrace?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM