Android NDK:从JNI C类调用java方法

前端之家收集整理的这篇文章主要介绍了Android NDK:从JNI C类调用java方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有JNI方法,我试图调用 Java方法.这是我的JNI代码
void DummySink::afterGettingFrame(unsigned frameSize,unsigned numTruncatedBytes,struct timeval presentationTime,unsigned /*durationInMicroseconds*/) {

     __android_log_print(ANDROID_LOG_VERBOSE,"RTSP","Frame: %c",propRec->sPropBytes);
     jmethodID mid;
     jclass handlerClass = env9->FindClass("ob/android/Stream");
     if (handlerClass == NULL) {
         __android_log_print(ANDROID_LOG_VERBOSE,"RTSP-Error","Class");
     }
     mid = env9->GetMethodID(handlerClass,"onResponse","([B)V");
     if (mid == NULL) {
         __android_log_print(ANDROID_LOG_VERBOSE,"Method");
     }

     jbyteArray jbArray = env9->NewByteArray((int)propRec->sPropLength);
     env9->SetByteArrayRegion(jbArray,(int)propRec->sPropLength,(jbyte*)propRec->sPropBytes);

     //jobject theClass = env9->FindClass("ob/android/Stream");

     env9->CallVoidMethod(handlerClass,mid,jbArray);

}

这是我有的Java代码

public void onResponse(byte[] str) 
    { 
        Log.v("Response","Java");
    }

我正在收到以下事故

03-08 16:01:05.915: W/dalvikvm(17552): JNI WARNING: can't call Lob/android/Stream;.onResponse on instance of Ljava/lang/Class;
03-08 16:01:05.915: W/dalvikvm(17552):              in Lob/android/Stream;.stream:()V (CallVoidMethodV)

在应用了Mah的回答之后,这是我收到的例外

03-08 16:40:20.646: W/dalvikvm(4076): JNI WARNING: JNI method called with exception pending
03-08 16:40:20.646: W/dalvikvm(4076):              in Lob/android/Stream;.stream:()V (NewByteArray)
03-08 16:40:20.646: W/dalvikvm(4076): Pending exception is:
03-08 16:40:20.646: I/dalvikvm(4076): java.lang.NoSuchMethodError: no method with name='onResponse' signature='([B)V' in class Lob/android/Stream;
03-08 16:40:20.646: I/dalvikvm(4076):   at ob.android.Stream.stream(Native Method)
03-08 16:40:20.646: I/dalvikvm(4076):   at ob.android.Stream.<init>(Stream.java:28)
03-08 16:40:20.654: I/dalvikvm(4076):   at ob.android.MainActivity.startRecording(MainActivity.java:203)

现在onResponse方法是静态的.

解决方法

您的Java方法是一种实例方法(不是静态的),但是您的本机代码并不指向任何调用onResponse()的ob.android.Stream的特定实例.

调用CallVoidMethod()时,第一个参数是要运行方法的实例(object),而不是类本身.如果您使用CallStaticVoidMethod(),该类本身将被使用.

原文链接:https://www.f2er.com/android/311423.html

猜你在找的Android相关文章