Thursday, December 23, 2010

[android-developers] JNI CallVoidMethod can t call

Hello, I tried to communicate between C++ and Java with JNI.

I manage to execute test like HelloJNI in android-ndk.

I want my c++ native code call java function but when I debug my app I
have this JNI warning:
"cant' call Lcom/project/teststl/TestSTL; .test on instance of Ljava/
lang/Class; "

my package is "com.project.teststl"
my java class is TestSTL and extends from Activity. in onCreate
function I call a native function write in c++ and all is ok.
In this native function I Find my class and GetMethodId without error.
But method CallVoidMethod crash. Why? must I create a thread and
getEnv?

Part of my native code:

jclass caller;


JNIEXPORT void JNICALL
Java_com_project_teststl_TestSTL_setCaller(JNIEnv* env, jobject obj)
{
if (env->ExceptionOccurred())
{
env->ExceptionDescribe();
__android_log_print(ANDROID_LOG_INFO, "test", "env null");
return ;
}
jclass localclass;// = (jclass)(env->GetObjectClass(obj));

localclass = env->FindClass("com/project/teststl/TestSTL");
if (localclass == 0)
{
__android_log_print(ANDROID_LOG_INFO, "test", "classe not found");

return ;
}

/* Create a global reference */
caller = (jclass*)(env->NewGlobalRef(localclass));

// use of global reference
jmethodID mid = (env)->GetMethodID( caller, "test", "()V");
if (mid == 0) {
return;
}

/* The local reference is no longer useful */
(env)->DeleteLocalRef( localclass);

// Here I have tried also with obj parameter

env->CallVoidMethod( caller, mid);

}

any idea?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment