2013-02-25 97 views
1

我想從jni類中調用一些非常簡單的java函數用於cocos2dx項目。但我一直收到錯誤有時在一些重要的調用中無法使用GetEnv()獲取環境,這會改變整個操作流程。無法使用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); 
} 

我使用JniHelper類從已被如下的cocos2d-x。

#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 

這工作有時,但我也有時得到上述錯誤。誰能告訴我如何解決這個問題。

+0

什麼是你回來的時候'GetEnv'失敗的實際錯誤代碼('jint')功能引起的? – 2013-02-25 08:20:02

+0

getEnv(JNIEnv ** env)返回布爾值而不是jint – glo 2013-02-25 08:22:51

+1

那麼,[文檔](http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/invocation.html#GetEnv )似乎不同意。 – 2013-02-25 09:02:51

回答

1

錯誤是因爲我是不是要求從主線程