title: JNI GetEnv() returns null in JNI_OnLoad
body: <p>For some reason, when I call <code>GetEnv()</code>, it returns NULL.
  I know that this is a normal behaviour when calling it from a thread that in not attached to the VM object but, in my case, I'm calling it from <code>JNI_OnLoad</code>, which is called when I call <code>System.loadLibrary</code> on the JAVA, should it shoult not be NULL.</p>
  
  <p>JNI.h :</p>
  
  <pre><code>class JNI
  {
  public:
  
      static jint OnLoad( JavaVM * pJavaVM, void * reserved );
  
      static JNIEnv * GetEnv();
  
      // Etc...
  };
  </code></pre>
  
  <p>JNI.cpp :</p>
  
  <pre><code>jint JNI::OnLoad( JavaVM * pJavaVM, void * reserved )
  {
      static bool bFirstCall = true;
  
      if( ! bFirstCall )
      {
          LOGCAT_E( "Tnx::JNI::OnLoad() - Cannot call this function more than once." );
          return -1;
      }
  
      bFirstCall = false;
  
      LOGCAT_I( "Tnx::JNI::OnLoad() - Begin, pJavaVM = %p.", pJavaVM );
  
      s_pJavaVM = pJavaVM;
  
      try
      {
          JNIEnv * pEnv = GetEnv(); // THROWS HERE
  
          // More stuff...
      }
      catch( const exception &amp; oExc )
      {
          LOGCAT_E( "Tnx::JNI::OnLoad() - Failed, exception : '%s'.", oExc.what() );
          return false;
      }
      catch( ... )
      {
          LOGCAT_E( "Tnx::JNI::OnLoad() - Failed, unknown exception." );
          return false;
      }
  
      LOGCAT_I( "Tnx::JNI::OnLoad() - End, success." );
      return JNI_VERSION_1_4;
  }
  
  JNIEnv * JNI::GetEnv()
  {
      JNIEnv * const pEnv = NULL;
  
      if( NULL == s_pJavaVM )
          throw runtime_error( "Tnx::JNI::GetEnv() - s_pJavaVM is NULL." );
  
      s_pJavaVM-&gt;GetEnv( (void **) &amp; pEnv, JNI_VERSION_1_4 );
  
      if( NULL == pEnv )
          throw runtime_error( "Tnx::JNI::GetEnv() - Failed to get JNI environment." );
  
      return pEnv;
  }
  
  jint JNICALL JNI_OnLoad( JavaVM * pJavaVM, void * reserved )
  {
      return Tnx::JNI::OnLoad( pJavaVM, reserved );
  }
  </code></pre>
  
  <p>.java :</p>
  
  <pre><code>public class XXXXActivity extends Activity
  {
      static
      {
          System.loadLibrary( "XXXXNative" );
      }
  
      private native static boolean initializeNative( XXXXActivity XXXXActivity, TermScreenView termScreenView );
  
      private native static boolean finalizeNative();
  
      private native boolean initializeInstance();
  
      private native boolean finalizeInstance();
  
      private TermScreenView mTermScreenView = null;
  
      @Override
      protected void onCreate( Bundle savedInstanceState )
      {
          Log.v( "", "XXXXActivity#onCreate() - Begin." );
  
          super.onCreate( savedInstanceState );
  
          try
          {
              this.setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE );
  
              setContentView( R.layout.activity_XXXX );
              mTermScreenView = (TermScreenView) findViewById( R.id.termScreenView );
          }
          catch( Exception exc )
          {
              exc.printStackTrace();
          }
  
          if( null == savedInstanceState )
          {
              Log.v( "", "XXXXActivity#onCreate() - First call to onCreate, initializing native layer." );
  
              if( ! (initializeNative( this, mTermScreenView ) &amp;&amp; initializeInstance()) )
                  Toast.makeText( getBaseContext(), "Failed to initialize", Toast.LENGTH_LONG ).show();
          }
  
          Log.v( "", "XXXXActivity#onCreate() - End." );
      }
  
      // Etc...
  }
  </code></pre>
  
  <p>Log :</p>
  
  <pre><code>10-01 13:07:14.460: D/dalvikvm(29694): Trying to load lib /data/data/XXXX.XXXX/lib/libXXXXNative.so 0x41970d10
  10-01 13:07:14.460: D/dalvikvm(29694): Added shared lib /data/data/XXXX.XXXX/lib/libXXXXNative.so 0x41970d10
  10-01 13:07:14.463: I/XXXX.XXXX(29694): Tnx::JNI::OnLoad() - Begin, pJavaVM = 0x40f34948.
  10-01 13:07:14.463: E/XXXX.XXXX(29694): Tnx::JNI::OnLoad() - Failed, exception : 'Tnx::JNI::GetEnv() - Failed to get JNI environment.'.
  10-01 13:07:14.463: W/dalvikvm(29694): JNI_OnLoad returned bad version (0) in /data/data/XXXX.XXXX/lib/libXXXXNative.so 0x41970d10
  10-01 13:07:15.123: D/dalvikvm(29694): threadid=1: still suspended after undo (sc=1 dc=1)
  </code></pre>
  
  <p>Any idea why it could be NULL ? Thanks.</p>