java - ClassCastException on JNI function call: CallVoidMethod -


i'm trying call java method native c++ code in android application. when try invoke java method exception occurs. i've looked through several examples, cant figure out im doing wrong. please me.

java side:

public class usbaudiorecorder {  private usbaudiorecordlistener listener;  usbaudiorecorder(usbaudiorecordlistener listener){     this.listener = listener; }      static {     system.loadlibrary("usbaudio"); }  public native int native_setupconnection(final int fd, final string usbfs_path);  public native void start();  public void received(byte[] audio){     listener.recorded(audio); } //some more methods... } 

native code:

static jclass class_usbaudiorecorder = null; static jmethodid methid_usbaudiorecorder_received;  jniexport jint jnicall java_com_example_soundcard_usbaudiorecorder_native_1setupconnection (jnienv *env , jobject obj, jint fd, jstring usbfs_path){  // objects class jclass clazz = env->getobjectclass(obj); if (!clazz) {     logd("could not find class");     error = libusb_error_other; } class_usbaudiorecorder =  (jclass) env->newglobalref(clazz);  env->deletelocalref(clazz);   // callbacks method id methid_usbaudiorecorder_received = env->getmethodid(class_usbaudiorecorder,"received", "([b)v"); if (!methid_usbaudiorecorder_received) {     logd("could not find method");     env->deleteglobalref((jobject) class_usbaudiorecorder);     error = libusb_error_other;     goto err; }  // event handle callback static void cb_trans(struct libusb_transfer* transfer){      // create jbytearray.     jbytearray  audioarray = env->newbytearray(packet_size * transfer->num_iso_packets);  //here populate jbytearray data  // callback env->callvoidmethod(class_usbaudiorecorder, methid_usbaudiorecorder_received, audioarray);  // cleaning env->deletelocalref(audioarray); jthrowable exc; exc = env->exceptionoccurred(); if (exc) {     env->exceptiondescribe();  } if (env->exceptioncheck()) {     logd("exception while trying pass sound data java");     env->exceptionclear();     return; } 

the exception output:

10-27 15:19:55.658 11890-12476/com.example.soundcard w/system.err: java.lang.classcastexception: [b cannot cast com.example.soundcard.usbaudiorecorder 10-27 15:19:55.658 11890-12476/com.example.soundcard w/system.err: @ java.lang.class.cast(class.java:1278) 10-27 15:19:55.663 11890-12476/com.example.soundcard w/system.err: @ com.example.soundcard.usbaudiorecorder.start(native method) 10-27 15:19:55.663 11890-12476/com.example.soundcard w/system.err: @ com.example.soundcard.mainactivity$onclicklistener$1.run(mainactivity.java:279) 10-27 15:19:55.663 11890-12476/com.example.soundcard w/system.err:
@ java.lang.thread.run(thread.java:841)

i have found solution. save others time: when calling nonstatic method need pass class instance callvoidmethod jobject. mistake call jclass, works static methods.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -