从 JNI 调用 Android 上的非静态方法
我想使用 JNI 在 Android 上调用非静态方法。我可以使用 CallStaticVoidMethod 调用静态方法。为了调用非静态方法,我使用了 CallVoidMethod。它不起作用。
谁能告诉我从 JNI 调用 Android 的非静态方法的正确代码吗?
jmethodID method = env->GetMethodID(gJniRefCached.ImsFwkLoaderClass, "DispVideo", "([BII)V");
env->CallVoidMethod(gJniRefCached.ImsFwkLoaderClass, 方法,arr,宽度,高度);
我也尝试过使用代码是的类的对象 jclass cls = env->GetObjectClass(obj); jmethodID method = env->GetMethodID(cls, "DispVideo", "([BII)V"); env->CallVoidMethod(cls, method,arr,width,height);
I want to call a non-static method on Android using JNI. I can call static methods using CallStaticVoidMethod
. To call non-static methods, I have used CallVoidMethod
. It is not working.
Can anybody please tell me correct code to call nonStatic method of Android From JNI?
jmethodID method = env->GetMethodID(gJniRefCached.ImsFwkLoaderClass, "DispVideo", "([BII)V");
env->CallVoidMethod(gJniRefCached.ImsFwkLoaderClass, method,arr,width,height);
I have also tried using object of class that code isjclass cls = env->GetObjectClass(obj);
jmethodID method = env->GetMethodID(cls, "DispVideo", "([BII)V");
env->CallVoidMethod(cls, method,arr,width,height);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了调用实例方法,您需要提供该方法所属类的实例,表示为
jobject
。但是,在这两个示例中,您都尝试使用类定义的实例(表示为jclass
)来调用实例方法。请尝试以下操作:
请注意第三行代码中的细微差别,其中我使用
obj
作为第一个参数,而不是cls
。您还可以在实例方法 JNI 函数的文档页面上看到这种差异: http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html#wp16656
看看
GetMethodID
和CallMethod
- 一个采用jclass
,另一个采用jobject
。In order to call an instance method, you need to provide an instance of the class the method belng to, represented as an
jobject
. However, in both examples you are trying to call the instance method with an instance of the class definition, represented as ajclass
.Try the following:
Note the subtle difference in the third line of code, where I use
obj
as the first parameter, instead ofcls
.You can see this difference also on the documentation page for the instance method JNI functions: http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html#wp16656
Look at both
GetMethodID
andCall<type>Method
- one takesjclass
, the other takesjobject
.