Reputation: 507
I'm trying to implement a callback function in JNI/NDK. I got error in GetMethodID function
I want to execute a function in Java (from C) that has the following signature:
[java code]
public int callback(String msg) {
cb_tv.setText(msg);
return 1;
}
[native C code]
JNIEXPORT void JNICALL Java_com_example_hellojni1_nativeJava_testDirectCallback(JNIEnv *env, jclass obj) {
static jmethodID cb = NULL;
jclass cls = (*env)->GetObjectClass(env, obj);
if(cb == NULL) {
cb = (*env)->GetMethodID(env, cls, "callback", "(Ljava/lang/String;)I");
if(cb == NULL) return;
}
(*env)->CallVoidMethod(env, obj, cb, (*env)->NewStringUTF(env, "[C->J] callback"));
return;
}
I get the following error:
08-01 16:26:43.617: W/dalvikvm(516): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-01 16:26:43.637: E/AndroidRuntime(516): FATAL EXCEPTION: main
08-01 16:26:43.637: E/AndroidRuntime(516): java.lang.NoSuchMethodError: callback
I don't understand the error. Is there anyone who can help me? Thank you in advance
Upvotes: 1
Views: 2861
Reputation: 78
Like fadden metioned...
Check that your Java_com_example_hellojni1_nativeJava_testDirectCallback is declared "native" and not "static native".
This was my problem.
Upvotes: 4