简体   繁体   English

创建时调用的JNI方法对象

[英]JNI method object called on creation

Using JNI I am storing a reference to a Java method object ( obj ) in order to call it later from C: 我使用JNI存储对Java方法对象( obj )的引用,以便以后从C中调用它:

 jobject obj = (*newEnv)->NewObject(newEnv, cls, mid);

where newEnv is my environment, cls is my class ID and mid is my method ID. 其中newEnv是我的环境, cls是我的类ID, mid是我的方法ID。

Everything works fine except that upon creating this object to be used as a reference later, it actually calls the method right away. 一切工作正常,除了在创建此对象以供以后用作引用时,它实际上立即调用了该方法。

I would like to just create the object for later and not call the method immediately. 我只想稍后创建该对象,而不立即调用该方法。

Thanks to Gabe Sechan's comments I was able to figure it out: 感谢Gabe Sechan的评论,我得以弄清楚:

jmethodID construct = (*newEnv)->GetMethodID(newEnv,cls,"<init>","()V");
jobject obj = (*newEnv)->NewObject(newEnv, cls, construct);

Then I can call the method I want using: 然后,我可以调用要使用的方法:

(*newEnv)->CallVoidMethod(newEnv, obj, mid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM