的Andr​​oid NDK:从JNI C级调用Java方法 [英] Android NDK: calling java method from JNI C class

查看:211
本文介绍了的Andr​​oid NDK:从JNI C级调用Java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JNI方法,其中我试图调用Java方法。这是我的JNI code

I have JNI method where I am trying to call Java method. Here is my JNI code

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, "RTSP-Error", "Method");
     }

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

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

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

}

下面是Java code,我有

Here is the Java code that I have

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

我收到以下崩溃

I am receiving following crash

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)

将马的回答后,这里是个例外,我接受

After applying Mah's answer, here is the exception I am receiving

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方法是静态的。

Now onResponse method is static.

推荐答案

您的Java方法是一个实例方法(而不是静态的),但你的本地code是不是指 ob.android.Stream 来调用 onResponse()

Your Java method is an instance method (not static), but your native code isn't referring to any specific instance of ob.android.Stream to call onResponse() for.

在调用 CallVoidMethod(),第一个参数是要实例(对象),你正在运行的方法对抗,而不是类本身。类本身会如果你使用可以使用 CallStaticVoidMethod()

When calling CallVoidMethod(), the first parameter is to be the instance (object) you're running the method against, not the class itself. The class itself would be used if you were using CallStaticVoidMethod().

这篇关于的Andr​​oid NDK:从JNI C级调用Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆