Calling simple java static method via JNI does not work, though c++ compiles and run it

自作多情 提交于 2019-12-11 10:50:06

问题


Considering this Java class with the static method:

public class TestClass{
    public string str;
    public TestClass() {
        str = "Test From Java";
    }
    public static String staticMethod() {
        return "Test From Java";
    }
}

I have written these lines of code in c++ file:

QAndroidJniObject str =  QAndroidJniObject::callStaticObjectMethod(
                                   "org/.../TestClass"
                                   ,"staticMethod"
                                   ,"(V)Ljava/lang/String;");

It seems everything is working but I don't know how can I use the str Object. I tried to convert it to a QString object using str.tostring() method but it always returns an empty string. Why it does not work as expected? I've also tested ()Ljava/lang/String; for method signature but no success!
Thanks in advance.


回答1:


You should specify the returned JNI type in <...> when calling the method :

QAndroidJniObject str =  QAndroidJniObject::callStaticObjectMethod<jstring>(
                               "org/.../TestClass"
                               ,"staticMethod");

QString string = str.toString();

Here there is no need to define the signature since your function has no argument.



来源:https://stackoverflow.com/questions/28693031/calling-simple-java-static-method-via-jni-does-not-work-though-c-compiles-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!