【发布时间】:2014-08-28 23:29:55
【问题描述】:
我已经在一个示例项目/插件上多次测试过它,它似乎工作得很好。一旦我开始在我的实际项目中集成,我就会收到一个错误(见下文)。我已经失去了大约两周的时间来尝试让 Unity / Android 插件工作,这是我得到的最远的。我感觉如此接近,但距离找到解决方案还很远!非常感谢我能得到的任何帮助。谢谢!
我得到的错误:
08-28 15:38:25.200: I/Unity(6191): AndroidJavaException: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
C#(统一):
// Update is called once per frame
void Update () {
testPlugin ();
printTestobj();
}
public void testPlugin() {
if (testobj == null) {
// First, obtain the current activity context
using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
playerActivityContext = actClass.GetStatic<AndroidJavaObject> ("currentActivity");
}
// Pass the context to a newly instantiated TestUnityProxy object
using (var pluginClass = new AndroidJavaClass("com.company.product.Main")) {
if (pluginClass != null) {
testobj = pluginClass.CallStatic<AndroidJavaObject> ("instance");
testobj.Call ("setContext", playerActivityContext);
}
}
}
}
Java(安卓):
public Main() {
INSTANCE = this;
}
public static Main instance() {
Log.i(TAG, "Inside instance.");
if (INSTANCE == null) {
Log.i(TAG, "instance b1.");
INSTANCE = new Main();
Log.i(TAG, "instance b2.");
}
Log.i(TAG, "instance b3.");
return INSTANCE;
}
public void setContext(Context ctx) {
this.context = ctx;
Log.i(TAG, "Application context set.");
}
【问题讨论】:
-
您应该向我们展示您的整个堆栈跟踪。只需在线程作业(Runnable、AsyncTask 等)开始时调用
Looper.prepare()。