Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QAndroidJniObject::callStaticMethod is returning 0.

    Mobile and Embedded
    2
    4
    883
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • I
      Ibrahim last edited by

      Hi; I am writing code about call a Java function from C++ but QAndroidJniObject is returning 0 value. (Note: I included QT += androidextras in .pro file).

      // Java Code
      package com.example.classes;
      public class JavaClassExample {
        public static int factorial(int n) {
          return (n > 1) ? n * factorial(n - 1) : 1;
        }
      }
      
      // C++ Code
      #include <QMessageBox>
      #include <QAndroidJniObject>
      ...
      int factorial(int n)
      {
        return QAndroidJniObject::callStaticMethod<jint>(
              "com/example/classes/JavaClassExample",
              "factorial",
              "(I)I",
              n);
      }
      ...
      QMessageBox::information(this, tr("Factorial"), QString::number(factorial(7)));
      // Output Always -> 0
      

      I am learning from here. Thanks.

      1 Reply Last reply Reply Quote 0
      • I
        Ibrahim last edited by

        Structure of my project and structure notification example of Qt: IMAGE LINK. I put .java file in Other Files directory but Qt example put .java file in Other file/android-sources/src/org/qtproject/example/notification directory. When I clicked Projects -> Build Android APK -> Create Templates button, created is Other files/android (like in image). Can be problem is so?

        1 Reply Last reply Reply Quote 0
        • Qojote
          Qojote last edited by

          Hi.
          I am working on a quite similar problem, so maybe this could help:

          Double-check your package names, i guess its good when the package name in your AndroidManifest.xml equals the packe in your Java class.
          I was able to call functions from java classes like shown on slide 42 of your posted link (extending with QtActivity -> remember to change your manifest!).

          Best regards

          I 1 Reply Last reply Reply Quote 1
          • I
            Ibrahim @Qojote last edited by Ibrahim

            @Qojote Thanks. I solved this problem: I put .java file in android/src/com/example/classes directory and it is running. Alright, how can I create src/com/example/classes directory? I do by manuel but this directory do not see in Qt Creator. Actually how can I create this directory from Qt Creator? Thanks.
            Edit:
            If you want to see directories, you must append this command in the .pro file:
            DISTFILES += path/to/directory
            Then close project and open project again.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post