Raneez Ahmed
Raneez Ahmed

Reputation: 3828

Invoking java method using jni

I have the following method in java which i try to call from cpp,

public void callback(short[] sArray) {
    Log.e("java", ""+sArray.length);
}

in cpp i tried this code to invoke the java method:

jclass cls = env->GetObjectClass(obj);
jmethodID mid =
env->GetMethodID(cls, "callback", "([S)V");
    if (mid == NULL) {
        return 0; // method not found
    }
     //short *sbuffer;
     //sbuffer is filled with some data
    env->CallVoidMethod(obj, mid,sbuffer);

but i got this error:

05-28 18:13:29.850: W/dalvikvm(18423): Invalid indirect reference 0x75402008 in decodeIndirectRef 05-28 18:13:29.850: E/dalvikvm(18423): VM aborting 05-28 18:13:29.850: A/libc(18423): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 18423 (ssconverterdemo)

What am i doing wrong here?

Upvotes: 0

Views: 1069

Answers (2)

kyriosli
kyriosli

Reputation: 333

Things like CallVoidMethod or else accepts parameters in format of jvalue. You need to translate your sbuffer into jshortArray and then make a jvalue representing it, then pass the jvalue entry to CallVoidMethod.

Upvotes: 0

Zang MingJie
Zang MingJie

Reputation: 5275

the method require an array parameter, you can't pass C array to java.

How to return an array from JNI to Java?

this explain how to pass array from jni to java

Upvotes: 2

Related Questions