JNI on Android, how to pass int from c to java

1,069 views
Skip to first unread message

Gioacchino

unread,
Dec 27, 2010, 5:20:28 PM12/27/10
to android-ndk
I have a C function, I simply returns an integer, as follows:

JNIEXPORT jint JNICALL Java_org_project_ScreenPosition(JNIEnv* env,
jobject thiz){
int i=1;
return i;
}

I call this function in the way of an Activity onCreateContextMenu
Android, as follows:

public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo){
menu.setHeaderTitle("TryMenu");

int a=ScreenPosition();
return;
}

But all crash, why?

风间

unread,
Dec 28, 2010, 12:09:25 AM12/28/10
to andro...@googlegroups.com
there's no doubt ur program is gonna crash,u really should check the online doc about NDK.
u should use ndk-build to build a shared-library in the first place,and then load the .so using System.load(XXX.so)
and also defined a native method in ur java code. then everything will go all right. there are lots of examples out there teaching u how to do this...piece of cake,trust me.


2010/12/28 Gioacchino <gioacchin...@gmail.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.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


Thomas

unread,
Dec 29, 2010, 2:21:47 AM12/29/10
to android-ndk
Post up a logcat, so people can see more details about your error.

jint should just be a typedef to int, but you could try declaring i as
a jint instead of an int.

Check your JNI glue code, I'm betting that's where the error is (I
don't see a native method declaration for ScreenPosition).
Reply all
Reply to author
Forward
0 new messages