Hi,
I need to be able to call Java from C code, and I have trouble getting
this to work in my sample program. Help :-)
Relevant code below. I tried with GetStaticMethodID +
CallStaticVoidMethod but that didn't work either (GetStaticMethodID
returns 0).
Also I'm not sure why this business with env vs lenv is happening. Code
was created by copying stuff from forum posts...
Danny
E/Yow ( 711): DoubleJniTest 1
E/Yow ( 711): DoubleJniTest NewStringUTF -> 0x43d114a0
E/Yow ( 711): DoubleJniTest GetObjectClass -> 0x43cffd00
E/Yow ( 711): DoubleJniTest GetStaticMethodID -> 0x412be468
I/DEBUG ( 27): *** *** *** *** *** *** *** *** *** *** *** *** ***
*** *** ***
I/DEBUG ( 27): Build fingerprint:
'generic/sdk/generic/:2.1/ERD79/22607:eng/test-keys'
I/DEBUG ( 27): pid: 711, tid: 711 >>>
net.sourceforge.projects.roadmap <<<
I/DEBUG ( 27): signal 11 (SIGSEGV), fault addr 00000004
I/DEBUG ( 27): r0 4104f100 r1 00000000 r2 40009408 r3 ad07ff50
<<RoadMap.java>>
package net.sourceforge.projects.roadmap;
...
public class RoadMap extends Activity
...
public void AddButton(String name) {
Button doit = new Button(thiz);
doit.setText(name);
buttons.addView(doit);
}
<< piece of jni/jni.c >>
static JavaVM *svm;
static jobject io;
jint
JNI_OnLoad(JavaVM *vm, void *reserved)
{
JNIEnv *env;
jclass cls;
jmethodID constr;
svm = vm;
if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_6) != JNI_OK)
return -1;
cls = (*env)->FindClass(env,
"net/sourceforge/projects/roadmap/RoadMap");
__android_log_print(ANDROID_LOG_ERROR, "JNI_OnLoad", "FindClass
-> %p", cls);
constr = (*env)->GetMethodID(env, cls, "<init>", "()V");
__android_log_print(ANDROID_LOG_ERROR, "JNI_OnLoad",
"GetMethodID -> %p", constr);
io = (*env)->NewGlobalRef(env, (*env)->NewObject(env, cls,
constr));
__android_log_print(ANDROID_LOG_ERROR, "JNI_OnLoad", "io -> %p",
io);
return JNI_VERSION_1_6;
}
void
JNI_OnUnload(JavaVM *vm, void *reserved)
{
JNIEnv *env;
(*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_6);
(*env)->DeleteGlobalRef(env, io);
}
void
Java_net_sourceforge_projects_roadmap_RoadMap_DoubleJniTest(JNIEnv* env,
jobject thiz, char *s)
{
JNIEnv *lenv;
jclass c;
jmethodID m;
char * ns;
__android_log_print(ANDROID_LOG_ERROR, "Yow", "DoubleJniTest
1");
if ((*svm)->GetEnv(svm, (void **)&lenv, JNI_VERSION_1_6) < 0) {
__android_log_print(ANDROID_LOG_ERROR, "Yow",
"DoubleJniTest error");
return;
}
ns = (*lenv)->NewStringUTF(lenv, "Yow baby");
__android_log_print(ANDROID_LOG_ERROR, "Yow", "DoubleJniTest
NewStringUTF -> %p", ns);
c = (*lenv)->GetObjectClass(lenv, io);
__android_log_print(ANDROID_LOG_ERROR, "Yow", "DoubleJniTest
GetObjectClass -> %p", c);
m = (*env)->GetMethodID(env, c, "AddButton",
"(Ljava/lang/String;)V");
__android_log_print(ANDROID_LOG_ERROR, "Yow", "DoubleJniTest
GetStaticMethodID -> %p", m);
(*env)->CallVoidMethod(env, c, m, ns);
__android_log_print(ANDROID_LOG_ERROR, "Yow", "DoubleJniTest
4");
}
--
Danny Backx ; danny.backx - at -
scarlet.be ;
http://danny.backx.info
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to
andro...@googlegroups.com.
To unsubscribe from this group, send email to
android-ndk...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/android-ndk?hl=en.