Qt不编译callStaticObjectMethod() [英] Qt does not compile callStaticObjectMethod()
问题描述
我写了以下代码从这个问题,它被编译和工作完美:
I wrote the following code from this question and it was compiled and worked perfectly:
QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod<jstring>(
"org/.../TestClass"
,"staticMethod");
现在我改变了java方法,它需要一个类型为string的输入参数。
Now I have changed the java method and it needs an input parameter of type string.
Java代码:
public class TestClass{
public string str;
public TestClass() {
str = "Test From Java";
}
public static String staticMethod(String str) {
return "Test From Java, "+str;
}
}
但是添加方法签名和输入参数不起作用为了我。我写了这个代码来使用JNI调用所提到的java类中的静态方法:
But adding the method signature and input parameter does not work for me. I wrote this code to invoke the static method within the mentioned java class using JNI:
QAndroidJniObject val = QAndroidJniObject::fromString("Test String");
QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod<jstring>(
"org/.../TestClass"
,"staticMethod"
,"(Ljava/lang/String;)Ljava/lang/String;"
,val.object<jstring>());
但是Qt的创建者不会构建它,打印此错误:
But Qt creator does not build it, printing this error:
...testclass.cpp:21: error:
no matching function for call to 'QAndroidJniObject::callStaticObjectMethod(
const char [36], const char [13], const char [39], _jstring*)'
,val.object<jstring>());
^
感谢任何帮助。
为此:
jstring str = QAndroidJniObject::callStaticMethod<jstring>(
"org/.../TestClass"
,"staticMethod"
,"(Ljava/lang/String;)Ljava/lang/String;"
,val.object<jstring>());
我收到以下错误:
... \testclass.cpp:21:错误:未定义引用'_jstring * QAndroidJniObject :: callStaticMethod <_jstring *>(char const *,char
const *,char const * ,...)'
...\testclass.cpp:21: error: undefined reference to '_jstring* QAndroidJniObject::callStaticMethod<_jstring*>(char const*, char const*, char const*, ...)'
##我也试过没有模板参数##的callStaticObjectMethod
## I also tried callStaticObjectMethod without template parameter ##
QAndroidJniObject str = QAndroidJniObject :: callStaticObjectMethod(org / ... TextClass,staticMethod,(Ljava / lang / String;)Ljava / lang / String; val.object());
QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("org/...TextClass" ,"staticMethod" ,"(Ljava/lang/String;)Ljava/lang/String;",val.object());
它总是返回一个空字符串。我不知道它是真的emty或不。我使用 qCritical()<< str.toString();
以打印该字符串,但打印一个空的qout然后!
It always returns an empty string. I'm not sure if it is really emty or not. I use qCritical() << str.toString();
to print the string but an empty qout is printed then!
推荐答案
尝试:
QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod(
"org/.../TestClass"
,"staticMethod"
,"(Ljava/lang/String;)Ljava/lang/String;"
,val.object<jstring>());
我认为这个函数不带模板参数。
I think this function doesn't take template parameter.
比你可以:
str.toString() //returns QString
并确保已将Java源文件导入到您的Android构建。例如,如果你的java类在 android-sources
文件夹下添加到你的.pro文件:
And make sure you have imported the Java source files to your android build. For example if your java classes are under android-sources
folder add this to your .pro file:
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
这篇关于Qt不编译callStaticObjectMethod()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!