an example code of GetMethodID

5,735 views
Skip to first unread message

Gabriel Huie

unread,
Dec 9, 2010, 9:14:12 PM12/9/10
to android-ndk
Hi I was wondering if anyone had an jni project example that had a
function in java being called from C. I looked up some examples online
but when I'm doing it my GetMethodID is returning 0 so i guess it
can't find the method I was hoping someone had an example using the
Android stuff in Eclipse maybe Im just not including a path or
something simple like that but just wondering if anyone has a simple
example so i can compare and see if im missing a step.

Onur Cinar

unread,
Dec 9, 2010, 9:33:36 PM12/9/10
to andro...@googlegroups.com
Hi Gabriel,

The signature should match, so that might be the issue. You may need
to use javap on command line:

javap -s -private com.my.Object

This should print the signatures for the methods in your class. After
you have the right signature, in JNI you can do the following:

jclass clazz = env->GetObjectClass(obj);

jmethodID myMethod = env->GetMethodID(clazz, "myMethod", "(III)V");

This should return a valid method id. If it doesn't, then could you
copy/paste the portion of your code, so we can troubleshoot?

Regards,

-onur

> --
> 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.
>
>

---
www.zdo.com

Gabriel Huie

unread,
Dec 13, 2010, 3:05:54 PM12/13/10
to android-ndk
Hey I hope you can help I havent been able to try your method yet
basically because I dont get how to use javap. I did what u said wtih
javap-s -private com.my.object

but basicaly it keeps saying it cant find that package or class
Where in the directory do u do for the eclipse like i really cant
figure this out

Onur Cinar

unread,
Dec 13, 2010, 11:59:19 PM12/13/10
to andro...@googlegroups.com

Hi Gabriel,

You need to run javap at the root level of your class files directory (the bin directory on Eclipse I believe).  Or you can use the -classpath argument to specify the classpath.

Regards,

-onur

Gabriel Huie

unread,
Dec 14, 2010, 1:35:45 PM12/14/10
to android-ndk
Okay so i think i got it right but it still not doing it and this is
in eclipse
public void callbacks(){
song_label.setText("jni");
}
this is the java code

then
this is my jni code
jstring
Java_com_bfrx_Bfrx_SSHDPlayFile( JNIEnv* env, jobject thiz, jstring
str){
char *nativeStr = (char*)env->GetStringUTFChars(str, 0);
jclass cls = env->GetObjectClass(thiz);
jmethodID mid = env->GetMethodID( cls, "callbacks", "()V");
//printf("%d the mid \n", cls);

env->CallVoidMethod( thiz, mid);
//printf("%s here in jni first \n", nativeStr);
//fclose (stdout);
return str;
}

the signature and method name is correct after doing the javap thing
soo the only thing i could think would be a reason for it not working
is maybe i need to do like
"Java_com_bfrx_Bfrx_callbacks"
I mean iono cuz i stll get a 0 in mid
On Dec 13, 8:59 pm, Onur Cinar <onur.ci...@gmail.com> wrote:
> Hi Gabriel,
>
> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>
> > .
> > > > For more options, visit this group athttp://
> > groups.google.com/group/android-ndk?hl=en.
>
> > > ---www.zdo.com
>
> > --
> > 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<android-ndk%2Bunsubscribe@googlegr oups.com>
> > .

Igor R

unread,
Dec 14, 2010, 1:46:39 PM12/14/10
to andro...@googlegroups.com
> Okay so i think i got it right but it still not doing it and this is
> in eclipse
>    public void callbacks(){
>        song_label.setText("jni");
>    }
> this is the java code
>
> then
> this is my jni code
> jstring
> Java_com_bfrx_Bfrx_SSHDPlayFile( JNIEnv* env, jobject thiz, jstring
> str){
>        char *nativeStr = (char*)env->GetStringUTFChars(str, 0);
>        jclass cls = env->GetObjectClass(thiz);
>        jmethodID mid = env->GetMethodID( cls, "callbacks", "()V");
> //printf("%d the mid \n", cls);
>
>        env->CallVoidMethod( thiz, mid);
> //printf("%s here in jni first  \n", nativeStr);
> //fclose (stdout);
>        return  str;
> }
>
> the signature and method name is correct after doing the javap thing
> soo the only thing i could think would be a reason for it not working
> is maybe i need to do like
> "Java_com_bfrx_Bfrx_callbacks"
> I mean iono cuz i stll get a 0 in mid

The GetMethodID parameters seem to be correct, but are you sure you
get the correct class with GetObjectClass? What do you see in logcat
output, does it say something when you call
GetObjectClass/GetMethodID?

Reply all
Reply to author
Forward
0 new messages