Cann't call Java method from Qt side...
-
Hi Guys!!
I wrote some java method, which always returns number 7, but when I call this method from c++ it returns 0..
where I have to create this file can you tell me??
because I think that QAndroidJniObject::callstaticmethod<jint>(Java_File_Path, "MethodName", "Signature");
here Java_File_Path is incorrect. -
@Taz742
you need to be a little more specific.
Show the Java code and the exact C++ code.
Are you really using the filepath on the disk inJava_File_Path
?!?! -
This post is deleted!
-
-
- is this really valid Java code?
- in your QAnadroidJNI call you specified to call a method with int parameter and int return value. Your java code doesn't have any parameters?!
- are you running this code on desktop machine or in an Android environment?!
- The java file needs to be bundled into your Android APK. Thus it needs to be compiled and be able to be found in the classpath to call it via the JVM runtime. But i don't see any android related files in your project?
-
@Taz742 to add to @raven-worx reply, it looks like your Java code won't compile, you're missing (); and the path to the Java class in C++ code shouldn't include "src"
Please take a look at this presentation from the creator of Qt's port to Android, the use case 1 is all you need.
-
@Taz742
Qt Android Extras module provides QAndroidJniObject class. Also Qt Android Extras module for just Android applications, not desktop applications.
You add this command intoapp.pro
file:android { QT += androidextras }
You must to add android template. So, you go to Projects -> choose Android x86 or Armv7 -> Build Android APK -> Click
Create Templates
button. You must to add java file intsrc
like this:
You can try this code (I wrote this code inmainwindow.cpp
):#include "mainwindow.h" #include "ui_mainwindow.h" #include <QAndroidJniObject> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); qDebug() << QAndroidJniObject::callStaticMethod<jint>( "com/example/Example", "fooMethod", "()I"); } MainWindow::~MainWindow() { delete ui; }