简体   繁体   中英

calling java method from c++ in qt

I am trying to call a method defined in android activity in c++ qt using QAndroidJniObject. here is my call in c++ class

QAndroidJniObject data =  QAndroidJniObject::callStaticObjectMethod("com/android/app/appActivity",
                                                                      "appData",
                                                                      "(I)Ljava/lang/String;");
QString dataValue = data.toString();
qDebug() <<"Data is " << dataValue;

this appData is defined in appActiviy android class and it returns a String this is defined method I want to call and get the returned string value

static  String appData(){
    Log.d("App Data is ", "Working");
    return data;
}

but I am getting null is dataValue and it is not throwing any error too.

You may need to manually check exceptions to get your Java errors.

From Qt documentation :

Handling Java Exception

When calling Java functions that might throw an exception, it is important that you check, handle and clear out the exception before continuing.

Note: It is unsafe to make a JNI call when there are exceptions pending.

void functionException()
{  
    QAndroidJniObject myString = QAndroidJniObject::fromString("Hello");  
    jchar c = myString.callMethod<jchar>("charAt", "(I)C", 1000);  
    QAndroidJniEnvironment env;
    if (env->ExceptionCheck()) {
        // Handle exception here.
        env->ExceptionClear();
    }
}

Are you sure you want to be calling com/android/app/appActivity and not com/android/app/Activity ?

Here are some thoughts:

  1. Have you used Log.d() to print the string before return making sure it is not null?

  2. Not sure it matters, but you are specifying an integer as argument, but the Java method does not have that in its signature. You should then provide this integer as a parameter in callStaticObjectMethod().

  3. As mentioned by Alex P, exceptions have to be handled or they will give you a headache as they might happen quite often and crash the entire application.

  4. I can not find any class at com/android/app/appActivity in the Android documentation. Did you mean com/android/app/Activity? If so, I can't find a method named "appData" here .

Thanks guys for your answer, finally I figured it out. It was way simple then I was trying

QAndroidJniObject data =  QAndroidJniObject::callStaticObjectMethod("com/android/app/appActivity",
                                                                  "appData",
                                                                  "(I)Ljava/lang/String;");

In this code I was not aware that this (I)Ljava/lang/String; means type of parameter your Java method is accepting but in my case there were none. So the correct answer is

QAndroidJniObject data =  QAndroidJniObject::callStaticObjectMethod<jstring>("com/android/app/appActivity",
                                                                  "appData")`

denotes return type of my defined java method. I guess it was silly mistake from my end...thanks again

I have the same problem today. Although it is different from your code, but the return value is also NULL.here is my code:

package org.test.project.test;
public class TestJava {
        public static String notify(int iNumber )
        {
            String strNum = iNumber+"";
            return strNum;
        }
}

and then the c++ code:

QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("org/test/project/test/TestJava",
                                              "notify",
                                              "(I)Ljava/lang/String;",
                                              m_iNumber);
//the str always been NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM