c# - Unity3d开发: JNI ERROR (app bug): accessed stale local reference 0x200001 (index 0 in a table of size 0)

标签 c# android unity3d java-native-interface

我在开发 unity3d 项目时使用 AndroidJavaObject。我尝试了一个非常简单的代码,如下所示,但它抛出了标题中的异常。

using UnityEngine;
using System.Collections;
using System.Threading;

public class MainScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
	}

    void OnGUI()
    {
        if (GUI.Button(new Rect(50, 50, 1000, 200), "Open Activity"))
        {
            Debug.Log("pressed");

            Thread t1 = new Thread(new ThreadStart(ListenThread));
            t1.IsBackground = false;
            t1.Start();
        }

        //quit  
        if (Input.GetKey(KeyCode.Escape) || Input.GetKey(KeyCode.Home))
        {
            Application.Quit();
        }
    }


    public static void ListenThread()
    {
        AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", "some_string");
        int hash = jo.Call<int>("hashCode");
        Debug.Log(hash);
    }


}

但是,如果我不像下面这样把AndroidJavaObject放在一个线程中,它会正常运行。

using UnityEngine;
using System.Collections;
using System.Threading;

public class MainScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
	}

    void OnGUI()
    {
        if (GUI.Button(new Rect(50, 50, 1000, 200), "Open Activity"))
        {
            Debug.Log("pressed");

            AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", "some_string");
            int hash = jo.Call<int>("hashCode");
            Debug.Log(hash);
        }

        //quit  
        if (Input.GetKey(KeyCode.Escape) || Input.GetKey(KeyCode.Home))
        {
            Application.Quit();
        }
    }


    public static void ListenThread()
    {
        AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", "some_string");
        int hash = jo.Call<int>("hashCode");
        Debug.Log(hash);
    }


}

但是在我的应用程序中,AndroidJavaObject 的一个阻塞函数将被调用,所以我必须使用线程。第一段代码有什么问题?是不是因为unity3d不支持线程中的AndroidJavaObject?请帮忙,谢谢!!

最佳答案

哈哈,我解决了。只需将线程附加到 AndroidJNI,因为线程需要附加到 JVM。代码如下:

public static void ListenThread()
{
  AndroidJNI.AttachCurrentThread();
  AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", "some_string");
  int hash = jo.Call<int>("hashCode");
  Debug.Log(hash);
  AndroidJNI.DetachCurrentThread();
}

不要忘记在线程即将结束时分离。

关于c# - Unity3d开发: JNI ERROR (app bug): accessed stale local reference 0x200001 (index 0 in a table of size 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39784677/

相关文章:

c# - C# 中的 C++ union — 奇怪的行为

c# - CA1303 从资源表中检索以下字符串而不是 : "Service Started"

c# - 通用列表的 list.RemoveAt(0) 有多贵?

android - 导航回 fragment 时 fragment 中的 Viewpager 未更新

unity3d - AudioSource.PlayOneShot音量增加和剪辑

asp.net - Reference.svcmap : Could not load file or assembly Microsoft. Practices.ServiceLocation,版本= 1.0.0.0

c# - 合并两个列表并组合对象值

java - 单击时如何更改变量并将其传递给android中的另一个 Activity (有代码)?

android - USB 系绳和调试同时进行

ios - Xcode 8.3 为 Unity IOS Build 制作了非常大的 (.ipa) 文件?