简体   繁体   English

通过JNI从C调用Java方法时,异常NOSUCHMETHODERROR

[英]Exception NOSUCHMETHODERROR when calling Java method from C through JNI

I have a problem sharing a stream between Java and C with JNI. 我在使用JNI在Java和C之间共享流时遇到问题。 I am trying to call a Java method from C, but I am getting a nosuchmethodfound exception when I am trying to get GetObjectMethodID . 我试图从C调用Java方法,但是当我尝试获取GetObjectMethodID nosuchmethodfound遇到了nosuchmethodfound异常。

C Code: C代码:

jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID aMethodID = (*env)->GetMethodID(env, cls, "callbackmethod", "(I)V");
if (aMethodID == 0) { 
    log("Unable to get methodID"); //Always getting aMethodID as 0
}
int myvar = 7;
(*env)->CallVoidMethod(env, obj, aMethodID, myvar);

Java Code: Java代码:

public class myclass extends activity {
    private void callbackmethod(int i) {
        Log.d("Tag", "In Java code");
    }

    -------
    more code
}

Now I am always getting an error Ljava/lang/NoSuchMethodError;: callbackmethod . 现在我总是遇到错误Ljava/lang/NoSuchMethodError;: callbackmethod

Everything looks fine, but my code doesn't work. 一切看起来不错,但我的代码无法正常工作。 What is the problem? 问题是什么?

Is the JNI method static by any chance? JNI方法是否是静态的? If so, its second parameter is the class, not the this pointer of the current object. 如果是这样,则其第二个参数是类,而不是当前对象的this指针。

Also, is the method in question in the current class or in its base? 另外,该方法是当前类还是其基类中的问题? Judging by the fact that you spelled activity in all lower case, this is not a copy/paste of your actual code, but a rewrite. 从您将activity拼写为小写的事实来看,这不是您的实际代码的复制/粘贴,而是重写。 It's easier to find errors in real code. 在真实代码中查找错误更容易。

It was my mistake. 是我的错 I was calling the native function from the java static function. 我从Java静态函数调用了本机函数。 I removed the static key word. 我删除了静态关键字。 It's working fine now. 现在工作正常。

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

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