简体   繁体   English

Unity to Android插件错误:无法在未调用Looper.prepare()的线程内创建处理程序

[英]Unity to Android plugin error: Can't create handler inside thread that has not called Looper.prepare()

I've tested this multiple times on an example project / plugin, and it seems to work perfectly. 我已经在一个示例项目/插件上对此进行了多次测试,并且看起来效果很好。 Once I begin integration on my actual project, I get an error (seen below). 一旦开始在实际项目中进行集成,就会出现错误(如下所示)。 I've lost about two weeks trying to get Unity / Android plugins working, and this is the furthest I've gotten. 为了使Unity / Android插件正常工作,我已经损失了大约两个星期的时间,这是我所获得的最大成果。 I feel so close, but so far from finding the solution! 我感觉很亲密,但距离寻找解决方案还很遥远! Any help I can get is greatly appreciated. 我能得到的任何帮助将不胜感激。 Thank you! 谢谢!

The error that I'm getting: 我得到的错误:

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() 08-28 15:38:25.200:I / Unity(6191):AndroidJavaException:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

C# (Unity): 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 (Android): Java(Android):

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.");
}

I don't know if this has been answered, but I'm adding this comment to help keep this issue alive. 我不知道是否已回答此问题,但我正在添加此评论以帮助使此问题持续存在。

The first step is to figure out what line of code the issue exists on. 第一步是找出问题所在的代码行。 And you don't know that because Unity won't tell you. 而且您不知道,因为Unity不会告诉您。

I am also developing Android plugins for Unity. 我也在为Unity开发Android插件。 One of the issues I'm seeing is that RuntimeException are caught by the Unity layer and the natural stacktrace seems to be replaced by something that Unity wants to insert. 我看到的问题之一是RuntimeException被Unity层捕获,并且自然的stacktrace似乎已被Unity要插入的东西代替。

Until Unit starts showing stacktraces instead of catching them and displaying something different, these kinds of things are going to be impossible to solve. 直到Unit开始显示堆栈跟踪而不是捕获它们并显示不同的内容之前,这些问题将无法解决。 After all how can you solve a bug if you have to guess what line of code an error occurred on? 毕竟,如果您必须猜测错误发生在哪一行代码上,那么如何解决错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Threading:遇到Java.Lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序 - System.Threading: Running into Java.Lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 在Xamarin中获取位置GPS数据时,无法在线程内创建处理程序 - Can't create handler inside thread when getting location GPS data in Xamarin Unity错误:只能从主线程调用InitWWW - Unity error: InitWWW can only be called from the main thread Unity Android:NoClassDefFoundError:无法创建通知 - Unity Android: NoClassDefFoundError: Can't create Notifications unity android 插件 c#- 找不到 dll 文件 - unity android plugin c#- can't find dll file 如何解决统一错误“只能从主线程调用调用”? - How to solve unity error “Invoke can only be called from the main thread”? 使用Unity(C#)创建Android插件 - Create Android plugin with Unity (C#) 无法在外部线程中调用InkPresenter - InkPresenter can't be called in external thread 为统一3D创建本机Flashlight Android Java插件 - Create a native Flashlight Android Java plugin for unity 3D Unity3D Android插件:无法启动服务,意图错误 - Unity3D Android plugin,: Unable to start service, intent error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM