1

I am trying to call a Java static function in Qt C++ class using QAndroidJniObject with a string parameter.

This is my Java class having function which i am calling

        public class StatusBar{

        public static void setStatusBarBackgroundColor(Activity activity,String colorPref) {

// My code


         }  
        }

I am calling this function from C++ as

void ECApplicationInfo::changeStatusBarColor(QString color)
{
  QAndroidJniObject::callStaticMethod<void>( 
                  "com/ezeecube/ezeesync/StatusBar",
                  "setStatusBarBackgroundColor",
                  "(Landroid/app/Activity;)V",
                   activity,color);
}

I am getting the following error

error: cannot pass objects of non-trivially-copyable type 'class QString' through '...' activity,color);

How can i get rid of this error

2 Answers 2

5

The definition of your function signature is not correct. You should also specify the second argument which has a type of Ljava/lang/String;. Also you should convert QString to jstring and the pass it as an argument :

QAndroidJniObject::callStaticMethod<void>( 
                  "com/ezeecube/ezeesync/StatusBar",
                  "setStatusBarBackgroundColor",
                  "(Landroid/app/Activity;Ljava/lang/String;)V",
                   activity,QAndroidJniObject::fromString(color).object<jstring>());
Sign up to request clarification or add additional context in comments.

4 Comments

Can i change orientation of android screen if it is fixed in androidManifest
I dont get any error but the function is also not getting called
I don't know how you are providing activity parameter. May be that's the problem.
@Nejat can you please help with This stackoverflow.com/questions/52289447/…
2

JNI doesn't understand QString, you need to convert it to JNI's jstring type. QAndroidJniObject has a handy static method for this:

QString q = "Hello world";
QAndroidJniObject jniObject = QAndroidJniObject::fromString(q);
jstring j = jniObject.object<jstring>();

This is fairly typical, JNI bridges generally require manual serialization between types.

QAndroidJniObject also provides a toString() method for converting from a Java string back to a QString.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.