title: Android Native Activity - Call Java method
body: <p>I've been trying for a while to call a java method from c++, I am able to do it successfully with an Activity class but with a NativeActivity, it crashes when calling CallVoidMethod. GetMethodID is able to find it, it returns an address. env &amp; vm objects are valid &amp; are populated from android_main(). Is it possible that it simply won't work with a native activity class?</p>
  
  <p>Cpp: (edited)</p>
  
  <pre><code>void SendNotification() {
  
      JavaVM* lJavaVM = main_activity-&gt;vm;
      JNIEnv* lJNIEnv = main_activity-&gt;env;
  
      JavaVMAttachArgs lJavaVMAttachArgs;
      lJavaVMAttachArgs.version = JNI_VERSION_1_6;
      lJavaVMAttachArgs.name = "NativeThread";
      lJavaVMAttachArgs.group = NULL;
  
      jint lResult = lJavaVM-&gt;AttachCurrentThread(&amp;lJNIEnv, &amp;lJavaVMAttachArgs);
      if (lResult == JNI_ERR)
          return;
  
      jobject lNativeActivity = main_activity-&gt;clazz;
      jclass ClassNativeActivity = lJNIEnv-&gt;GetObjectClass(main_activity-&gt;clazz);
      jmethodID _method = lJNIEnv-&gt;GetMethodID(ClassNativeActivity, "SendNotification", "()V");
  
      lJNIEnv-&gt;CallVoidMethod(lNativeActivity, _method);
  
      lJavaVM-&gt;DetachCurrentThread();
  }
  </code></pre>
  
  <p><br>
  Java:</p>
  
  <pre><code>package com.thor.kalen;
  
  import android.app.AlertDialog;
  import android.app.NativeActivity;
  import android.os.Bundle;
  
  public class MainActivity extends NativeActivity
  {
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
      } // End of public void onCreate(Bundle savedInstanceState)
  
      public void SendNotification() {
          new AlertDialog.Builder(this).setTitle("Alert").setMessage("").setNeutralButton("Close", null).show();
      }
  }
  </code></pre>