通过 JNI 调用简单的 java 静态方法不起作用,尽管 C++ 编译并且 运行 它

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

用静态方法考虑这个Javaclass:

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

我已经在 C++ 文件中编写了这些代码行:

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

似乎一切正常,但我不知道如何使用 str 对象。我尝试使用 str.tostring() 方法将它转换为 QString 对象,但它总是 returns 一个空字符串。 为什么它不能按预期工作?我还测试了 ()Ljava/lang/String; 方法签名但没有成功!
提前致谢。

调用方法时,需要在<...>中指定返回的JNI类型:

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

QString string = str.toString();

这里不需要定义签名,因为你的函数没有参数。