Results 1 to 1 of 1

Thread: How to call a static method of java from c++ side?

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to call a static method of java from c++ side?

    simpleJNI.hpp

    Qt Code:
    1. #include <QObject>
    2.  
    3. class simpleJNI : public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit simpleJNI(QObject *parent = nullptr);
    8.  
    9. Q_INVOKABLE QString printHelloWorld();
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    simpleJNI.cpp

    Qt Code:
    1. #include "simpleJNI.hpp"
    2.  
    3. #include <QtAndroidExtras/QAndroidJniObject>
    4.  
    5. simpleJNI::simpleJNI(QObject *parent) :
    6. QObject(parent)
    7. {
    8. }
    9.  
    10.  
    11. QString simpleJNI::printHelloWorld()
    12. {
    13. QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("adv/appBucksAdv",
    14. "getString",
    15. "(V)Ljava/lang/String;"
    16. );
    17.  
    18. return str.toString();
    19. }
    To copy to clipboard, switch view to plain text mode 

    appBucksAdv.java

    Qt Code:
    1. //package adv;
    2.  
    3. import org.qtproject.qt5.android.bindings.QtApplication;
    4. import org.qtproject.qt5.android.bindings.QtActivity;
    5. //import android.content.Context;
    6.  
    7. public class appBucksAdv extends QtActivity
    8. {
    9. public static String getString()
    10. {
    11. return "hello world";
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    main.qml

    Qt Code:
    1. import QtQuick 2.2
    2. import QtQuick.Controls 1.1
    3.  
    4. Rectangle {
    5. width: 100
    6. height: 62
    7.  
    8. color: "red"
    9.  
    10. Text{
    11. id: text
    12. height: 30
    13. width: parent.width
    14. anchors.centerIn: parent
    15. }
    16.  
    17. MouseArea{
    18. anchors.fill: parent
    19.  
    20. onClicked: {
    21. text.text = text.text + simpleJNI.printHelloWorld() + "\n"
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    But the program do not print “helloWorld\n”

    message :
    W/dalvikvm( 6727): Bogus method descriptor: (V)Ljava/lang/String;

    Layout of the project https://github.com/stereomatchingkis...er/androidTest


    Added after 28 minutes:


    If I change the signature of the function to

    Qt Code:
    1. public static String printHelloWorld(int a)
    2. {
    3. return "hello world";
    4. }
    To copy to clipboard, switch view to plain text mode 

    and simpleJNI.cpp to

    Qt Code:
    1. QString simpleJNI::printHelloWorld()
    2. {
    3. QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("adv/appBucksAdv",
    4. "printHelloWorld",
    5. //change (V) to (I)
    6. "(I)Ljava/lang/String;",
    7. 23);
    8.  
    9. return str.toString();
    10. }
    To copy to clipboard, switch view to plain text mode 


    The codes work
    Last edited by stereoMatching; 31st December 2013 at 17:23.

Similar Threads

  1. Side effect to const static variable - do not know how
    By Prototyp144 in forum General Programming
    Replies: 0
    Last Post: 4th June 2013, 15:25
  2. Call Non-static method on static method
    By METEOR7 in forum Qt Programming
    Replies: 7
    Last Post: 3rd May 2012, 22:26
  3. How to call a Qt function from Java swing
    By tatung in forum Newbie
    Replies: 1
    Last Post: 10th April 2012, 22:01
  4. C++ method call
    By ^NyAw^ in forum General Programming
    Replies: 4
    Last Post: 19th November 2011, 13:19
  5. Replies: 0
    Last Post: 4th August 2011, 12:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.