【问题标题】:Unity to Android plugin error: Can't create handler inside thread that has not called Looper.prepare()Unity 到 Android 插件错误:无法在未调用 Looper.prepare() 的线程内创建处理程序
【发布时间】: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()

标签: c# android unity3d


【解决方案1】:

我不知道这是否已得到解答,但我添加此评论以帮助保持此问题的存在。

第一步是找出问题所在的代码行。你不知道,因为 Unity 不会告诉你。

我还在为 Unity 开发 Android 插件。我看到的问题之一是 Unity 层捕获了 RuntimeException,并且自然堆栈跟踪似乎被 Unity 想要插入的东西所取代。

在 Unit 开始显示堆栈跟踪而不是捕获它们并显示不同的东西之前,这类事情将无法解决。毕竟,如果您必须猜测错误发生在哪一行代码上,您如何解决错误?

【讨论】:

    猜你喜欢
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    相关资源
    最近更新 更多