i am trying to make callbacks to my android application from a native
C thread using JNI. however when i call FindClass it returns a NULL
value. i need to get the jclass value returned by FindClass() to
call GetStaticMethoID to make calls back up to my android application.
i store the JavaVM* globally that i receive from JNI_OnLoad which gets
called from the android thread when my application loads my shared
library. I then use that JavaVM* to attach the java virtual machine
to my native C thread by calling AttachCurrentThread() which returns a
0 value indicating success.
the weird thing is that when i make calls to FindClass() in the
android thread it works fine and returns a valid value. however when
i call FindClass() from the native C thread it returns NULL. I was
thinking that it might have something to do with the native C thread
not have the proper classpath set up? any help would be greatly
appreciated.
my code is down below, i omitted error checking and some of the code
to save space.
#include "JNIHelp.h"
#include "jni.h"
#include "utils/Log.h"
static JavaVM *jvm;
static void androidCback(int event)
{
jint r;
jclass cls;
JNIEnv *env;
jmethodID mid;
r = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
/* !!! FindClass returns NULL !!! */
cls = (*env)->FindClass(env, "com.android.SomeApp.Class");
mid = (*env)->GetStaticMethodID(env, cls, "handleEvent", "(I)V");
(*jvm)->DetachCurrentThread(jvm);
}
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
JNIEnv *env;
JNINativeMethod meth;
jclass k;
jint r;
r = (*vm)->GetEnv (vm, (void **) &env, JNI_VERSION_1_4);
/* !!! FindClass returns valid value !!! */
k = (*env)->FindClass (env, "com.android.SomeApp.Class");
/* save the java virtual machine globally */
jvm = vm;
meth.name = "enableNative";
meth.signature = "(I)Z";
meth.fnPtr = enableNative;
r = (*env)->RegisterNatives (env, k, &meth, 1);
}
JNIEXPORT void JNI_OnUnload(JavaVM *vm, void *reserved)
{
JNIEnv *env;
jclass k;
jint r;
r = (*vm)->GetEnv (vm, (void **) &env, JNI_VERSION_1_4);
/* !!! FindClass returns valid value !!! */
k = (*env)->FindClass (env, "com.android.SomeApp.Class");
(*env)->UnregisterNatives(env, k);
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---