Qt does not compile callStaticObjectMethod()

≯℡__Kan透↙ 提交于 2020-01-05 13:14:29

问题


I wrote the following code from this question and it was compiled and worked perfectly:

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

Now I have changed the java method and it needs an input parameter of type string.

The Java code:

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

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>());

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>());
                                                                              ^

Thanks for any help.

I also tried callStaticMethod

For this:

jstring str = QAndroidJniObject::callStaticMethod<jstring>(
                      "org/.../TestClass"
                      ,"staticMethod"
                      ,"(Ljava/lang/String;)Ljava/lang/String;"
                      ,val.object<jstring>());

I got the following error :

...\testclass.cpp:21: error: undefined reference to '_jstring* QAndroidJniObject::callStaticMethod<_jstring*>(char const*, char const*, char const*, ...)'

## I also tried callStaticObjectMethod without template parameter ##

QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("org/...TextClass" ,"staticMethod" ,"(Ljava/lang/String;)Ljava/lang/String;",val.object());

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!


回答1:


Try:

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.

Than you can do:

str.toString() //returns QString

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


来源:https://stackoverflow.com/questions/28695794/qt-does-not-compile-callstaticobjectmethod

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