0

Please help to call java void method(non static) from C++, It simple sdl2 android project . I'am trynig many times but can't make it works:-(. CallVoidMethod always crashes.

C++ code:

JNIEnv* Android_JNI_GetEnv(void) {
    JNIEnv *env;
    int status = mJavaVM->AttachCurrentThread(&env, NULL);
    if(status < 0) {
        LOGE("failed to attach current thread");
        return 0;
    }

    return env;
}

int Android_JNI_SetupThread(void) {

    JNIEnv *env = Android_JNI_GetEnv();
    pthread_setspecific(mThreadKey, (void*) env);
    return 1;
}   

jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv *env;
    mJavaVM = vm;
    LOGI("JNI_OnLoad called");
    if (mJavaVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        LOGE("Failed to get the environment using GetEnv()");
        return -1;
    }
        Android_JNI_SetupThread();


    return JNI_VERSION_1_4;
}


void B_Init(JNIEnv* mEnv, jclass cls, jobject obj)
{
    Android_JNI_SetupThread();

    mActivityClass = (jclass)mEnv->NewGlobalRef(cls);

}


extern "C" void Java_org_libsdl_app_SDLActivity_nativeInitB(JNIEnv* env, jclass cls, jobject obj)
{
    /* This interface could expand with ABI negotiation, calbacks, etc. */
    B_Init(env, cls, obj);

}

    void Java_org_libsdl_app_SDLActivity_nativeDemoInit()
    {

    JNIEnv* env = Android_JNI_GetEnv();

    jclass cls;

    jmethodID mid;

    cls =  env->FindClass("org/libsdl/app/SDLActivity");

    mid = env->GetMethodID(cls, "displayInterstitial2", "()V");

    **env->CallVoidMethod(cls, mid2);**

    } 
}

Java code(org/libsdl/app/SDLActivity):

/**
    SDL Activity
**/
public class SDLActivity extends Activity {


public void displayInterstitial2() {

            some code;

    }

}

I'm trying next to simplify code

C++ code:

JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_nativeinitB(JNIEnv* env, jobject obj)
{
    jclass clz =  env->GetObjectClass(obj);  // instead of FindClass
    jmethodID mid = env->GetMethodID(clz, "displayInterstitial2", "()V");
   // if(!mid) return;  // method does not exist, should write some logs
   env->CallVoidMethod(obj, mid); 
}

and get error: No implementation for native Lorg/libsdl/app/SDLActivity;.nativeInitB:()V

2
  • I can see your method name is displayInterstitial and you call displayInterstitial2 from C++. Any explanation for that? Commented Jul 21, 2014 at 12:23
  • Sorry in real code it's displayInterstitial2 in java. Commented Jul 21, 2014 at 12:24

1 Answer 1

1

If you call native method from your activity, you can try this way:

JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_nativeDemoInit(JNIEnv* env, jobject obj){
   jclass cls = (*env)->GetObjectClass(env,obj);  // instead of FindClass
   jmethodID mid = (*env)->GetMethodID(env, cls, "displayInterstitial2", "()V");
   if(!mid) return;  // method does not exist, should write some logs
   (*env)->CallVoidMethod(env, obj, mid);
}

in Activity class:

 public void displayInterstitial2(){
    Log.d("call displayInterstitial2");
 }

 public native void nativeDemoInit();

Reference from this and this

Sign up to request clarification or add additional context in comments.

4 Comments

GetMethodID gives me an error can find method in class Ljava/lang/CLass, but i call nativeDemoInit in SDLActivity. and displayInterstitial2 in SDLActivity.
I updated my answer with workable code. You can try it
after trying this I have an error java.lang.UnsatisfiedLinkError: Native method not found: org.libsdl.app.SDLActivity.nativeDemoInit:()V
Please check your package for correction. Make sure your SDLActivity is in org.libsdl.app package. If still not work, please update your full source code (include package and method prototype)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.