title: Failed to get the environment using GetEnv()
body: <p>I am trying to call some very simple java functions from jni class for a cocos2dx project. But i keep getting the error <strong>Failed to get the environment using GetEnv()</strong> on some important calls at times and this changes whole flow of operation. </p>
  
  <p>I am making JNI calls in following way </p>
  
  <pre><code>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-&gt;NewStringUTF(key);
      jstring stringArg2 = methodInfo.env-&gt;NewStringUTF(value);
      methodInfo.env-&gt;CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg, stringArg2);
      methodInfo.env-&gt;DeleteLocalRef(stringArg);
      methodInfo.env-&gt;DeleteLocalRef(stringArg2);
      methodInfo.env-&gt;DeleteLocalRef(methodInfo.classID);
  }
  </code></pre>
  
  <p>I am using JniHelper class from cocos2d-x that has been given below.</p>
  
  <pre><code> #include "JniHelper.h"
  #include &lt;android/log.h&gt;
  #include &lt;string.h&gt;
  
  #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-&gt;GetEnv((void**)env, JNI_VERSION_1_4) != JNI_OK)
          {
              LOGD("Failed to get the environment using GetEnv()");
              break;
          }
  
          if (JAVAVM-&gt;AttachCurrentThread(env, 0) &lt; 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(&amp;pEnv))
              {
                  break;
              }
          }
  
          ret = pEnv-&gt;FindClass(className);
          if (! ret)
          {
               LOGD("Failed to find class of %s", className);
              break;
          }
      } while (0);
  
      return ret;
  }
  
  static bool getStaticMethodInfo_(cocos2d::JniMethodInfo &amp;methodinfo, const char *className, const char *methodName, const char *paramCode)
  {
      jmethodID methodID = 0;
      JNIEnv *pEnv = 0;
      bool bRet = false;
  
      do
      {
          if (! getEnv(&amp;pEnv))
          {
              break;
          }
  
          jclass classID = getClassID_(className, pEnv);
  
          methodID = pEnv-&gt;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 &amp;methodinfo, const char *className, const char *methodName, const char *paramCode)
      {
          jmethodID methodID = 0;
          JNIEnv *pEnv = 0;
          bool bRet = false;
  
          do
          {
              if (! getEnv(&amp;pEnv))
              {
                  break;
              }
  
              jclass classID = getClassID_(className, pEnv);
  
              methodID = pEnv-&gt;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(&amp;env))
          {
              return 0;
          }
  
          const char* chars = env-&gt;GetStringUTFChars(jstr, NULL);
          string ret(chars);
          env-&gt;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 &amp;methodinfo, const char *className, const char *methodName, const char *paramCode)
  {
      return getStaticMethodInfo_(methodinfo, className, methodName, paramCode);
  }
  
  bool JniHelper::getMethodInfo(JniMethodInfo &amp;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
  </code></pre>
  
  <p>This works at times but i get the above mentioned error at times too. Can anyone tell me how to solve this.</p>