通过JNI调用简单的Java静态方法是行不通的,虽然C ++编译和运行它 [英] Calling simple java static method via JNI does not work, though c++ compiles and run it

查看:141
本文介绍了通过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.

这篇关于通过JNI调用简单的Java静态方法是行不通的,虽然C ++编译和运行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆