0

I am trying to call non static method callNative of my own Java class from QT/C++ but I get the following error:

W System.err: java.lang.NoSuchMethodError: no non-static method "Lorg/qtproject/qt5/android/bindings/QtActivity;.callNative()V;"

Here is the code of java class:

package com.ieio.finger;


import static java.lang.System.out;

public class Finger extends org.qtproject.qt5.android.bindings.QtActivity {
    public void callNative()
        {
            System.out.println("I am in Java");
        }
}

Here the C++ code finger.h

#ifndef FINGER_H
#define FINGER_H

#include <QObject>

class finger : public QObject
{
    Q_OBJECT
public:
    explicit finger(QObject *parent = nullptr);
    Q_INVOKABLE void call_java();
signals:

public slots:
};

#endif // FINGER_H

finger.cpp:

#include <QtAndroidExtras/QAndroidJniObject>
#include <QtAndroidExtras/QtAndroid>
#include "finger.h"


finger::finger(QObject *parent) : QObject(parent)
{

}

void finger::call_java()
{
    qDebug()<< "call java";
    QtAndroid::androidActivity().callObjectMethod("callNative","()V;");
    qDebug()<< "called java";
}

main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <finger.h>

    int main(int argc, char *argv[])
    {    
        QGuiApplication app(argc, argv);

        QQmlApplicationEngine engine;
        qmlRegisterType<finger>("io.qt.ieio.finger", 1, 0, "Finger");

        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;

        return app.exec();
    }

and the qml:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2

import io.qt.ieio.finger 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")


    Finger
    {
        id:myFinger
    }

    Button {
        id: button
        x: 234
        y: 284
        text: qsTr("Button")
        onClicked: {
            myFinger.call_java()
        }
    }
}

Any ideas why I get java.lang.NoSuchMethodError?

4
  • Possible duplicate of Calling Java function from Qt C++ Commented Feb 20, 2018 at 11:24
  • I do not think so, in the link you suggested all the Java function are static. Commented Feb 20, 2018 at 13:47
  • the semicolon after ()V does not belong there. Commented Feb 20, 2018 at 14:18
  • yes, right this is a mistake Commented Feb 21, 2018 at 8:47

1 Answer 1

2

Eventually I found a solution: I changed the method:

void finger::call_java()
{
    qDebug()<< "call java";
    QtAndroid::androidActivity().callObjectMethod("callNative","()V;");
    qDebug()<< "called java";
}

in

void finger::call_java()
{
    qDebug()<< "call java";
    QtAndroid::runOnAndroidThread([]{
        QAndroidJniObject myJavaObject("com/ieio/finger/Finger");
        myJavaObject.callMethod<void>("callNative","()V");
    });
    qDebug()<< "called java";
}
Sign up to request clarification or add additional context in comments.

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.