java - 使用 GetEnv() 获取环境失败

标签 java c++ java-native-interface cocos2d-x

我正在尝试从 cocos2dx 项目的 jni 类中调用一些非常简单的 java 函数。但我有时在一些重要的调用中不断收到错误无法使用 GetEnv() 获取环境,这会改变整个操作流程。

我正在通过以下方式进行 JNI 调用

void SaveStringJni(const char *key, const char *value)
{
    cocos2d::JniMethodInfo methodInfo;
    if (! JniHelper::getStaticMethodInfo(methodInfo, CLASS_NAME, "SaveString", "(Ljava/lang/String;Ljava/lang/String;)V"))
    {
        return;
    }
    jstring stringArg = methodInfo.env->NewStringUTF(key);
    jstring stringArg2 = methodInfo.env->NewStringUTF(value);
    methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg, stringArg2);
    methodInfo.env->DeleteLocalRef(stringArg);
    methodInfo.env->DeleteLocalRef(stringArg2);
    methodInfo.env->DeleteLocalRef(methodInfo.classID);
}

我正在使用下面给出的 cocos2d-x 中的 JniHelper 类。

 #include "JniHelper.h"
#include <android/log.h>
#include <string.h>

#if 1
#define LOG_TAG "JniHelper"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
#define LOGD(...)
#endif

#define JAVAVM cocos2d::JniHelper::getJavaVM()

using namespace std;

extern "C"
{

//////////////////////////////////////////////////////////////////////////
// java vm helper function
//////////////////////////////////////////////////////////////////////////

static bool getEnv(JNIEnv **env)
{
    bool bRet = false;

    do
    {
        if (JAVAVM->GetEnv((void**)env, JNI_VERSION_1_4) != JNI_OK)
        {
            LOGD("Failed to get the environment using GetEnv()");
            break;
        }

        if (JAVAVM->AttachCurrentThread(env, 0) < 0)
        {
            LOGD("Failed to get the environment using AttachCurrentThread()");
            break;
        }

        bRet = true;
    } while (0);

    return bRet;
}

static jclass getClassID_(const char *className, JNIEnv *env)
{
    JNIEnv *pEnv = env;
    jclass ret = 0;

    do
    {
        if (! pEnv)
        {
            if (! getEnv(&pEnv))
            {
                break;
            }
        }

        ret = pEnv->FindClass(className);
        if (! ret)
        {
             LOGD("Failed to find class of %s", className);
            break;
        }
    } while (0);

    return ret;
}

static bool getStaticMethodInfo_(cocos2d::JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode)
{
    jmethodID methodID = 0;
    JNIEnv *pEnv = 0;
    bool bRet = false;

    do
    {
        if (! getEnv(&pEnv))
        {
            break;
        }

        jclass classID = getClassID_(className, pEnv);

        methodID = pEnv->GetStaticMethodID(classID, methodName, paramCode);
        if (! methodID)
        {
            LOGD("Failed to find static method id of %s", methodName);
            break;
        }

        methodinfo.classID = classID;
        methodinfo.env = pEnv;
        methodinfo.methodID = methodID;

        bRet = true;
    } while (0);

    return bRet;
}

    static bool getMethodInfo_(cocos2d::JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode)
    {
        jmethodID methodID = 0;
        JNIEnv *pEnv = 0;
        bool bRet = false;

        do
        {
            if (! getEnv(&pEnv))
            {
                break;
            }

            jclass classID = getClassID_(className, pEnv);

            methodID = pEnv->GetMethodID(classID, methodName, paramCode);
            if (! methodID)
            {
                LOGD("Failed to find method id of %s", methodName);
                break;
            }

            methodinfo.classID = classID;
            methodinfo.env = pEnv;
            methodinfo.methodID = methodID;

            bRet = true;
        } while (0);

        return bRet;
    }

    static string jstring2string_(jstring jstr)
    {
        if (jstr == NULL)
        {
            return "";
        }

        JNIEnv *env = 0;

        if (! getEnv(&env))
        {
            return 0;
        }

        const char* chars = env->GetStringUTFChars(jstr, NULL);
        string ret(chars);
        env->ReleaseStringUTFChars(jstr, chars);

        return ret;
    }
}

NS_CC_BEGIN

JavaVM* JniHelper::m_psJavaVM = NULL;

JavaVM* JniHelper::getJavaVM()
{
    return m_psJavaVM;
}

void JniHelper::setJavaVM(JavaVM *javaVM)
{
    m_psJavaVM = javaVM;
}

string JniHelper::m_externalAssetPath;

const char* JniHelper::getExternalAssetPath() {
    return m_externalAssetPath.c_str();
}

void JniHelper::setExternalAssetPath(const char * externalAssetPath) {
    m_externalAssetPath = externalAssetPath;
}

jclass JniHelper::getClassID(const char *className, JNIEnv *env)
{
    return getClassID_(className, env);
}

bool JniHelper::getStaticMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode)
{
    return getStaticMethodInfo_(methodinfo, className, methodName, paramCode);
}

bool JniHelper::getMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode)
{
    return getMethodInfo_(methodinfo, className, methodName, paramCode);
}

string JniHelper::jstring2string(jstring str)
{
    return jstring2string_(str);
}

NS_CC_END

这有时有效,但我有时也会遇到上述错误。谁能告诉我如何解决这个问题。

最佳答案

该错误是由于我没有从主线程调用该函数而引起的

关于java - 使用 GetEnv() 获取环境失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15061763/

相关文章:

c++ - 在 mmap 内存中有效分配动态数组

rust - 尝试在 JNI 中创建字符串并将其添加到 Arraylist

javascript - 使用 Selenium 和 java 测试 React JS 下拉列表

java - Float - 数据类型混淆

java - JNI 如何从 GetDirectBufferAddress 递增指针

c++ - 为什么使用静态 "constants"而不是实际值?

c++ - 在继承类中没有匹配的泛型委托(delegate)作为 lambda 参数

javascript - Android JNI JavascriptCore JSEvaluateScript 随机给出解析错误

java - JNI RegisterNatives 不适用于 ClassLoader.loadClass() 加载的类

java - 如何使用 Joda 计算出 UTC 偏移量?