通过JNI调用简单的Java静态方法是行不通的,虽然C ++编译和运行它

问题描述:

考虑使用静态方法这个Java类:

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";
    }
}

我已经用C写code的这些行++文件:

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

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

似乎一切正常,但我不知道我怎么可以使用 STR 对象。我试图将其转换为使用 str.tostring()方法的的QString 对象,但它总是返回一个空字符串。
为什么预期它不工作?我还测试了()Ljava /朗/字符串;!的方法签名,但没有成功,结果
先谢谢了。

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.

您应该指定返回JNI类型< ...>调用方法时

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.