[HELP] QT6 calling android java from c++
-
Hi, i would like to ask for a help , this is my first time using QT and i have a problem using it, before i switch my existing project to QT i want to make this work
I have below folder structure
[Test]
[android]
- AndroidManifest.xml
[com]
[sample]
[test1]
-CustomToast.java
- main.cpp
- main.qml
- test1.pro
- test1.pro.usermy test1.pro contains the following code
QT += quickSOURCES +=
main.cppresources.files = main.qml
resources.prefix = /$${TARGET}
RESOURCES += resourcesAdditional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetandroid {
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android QT += core-private DISTFILES += android/com/sample/test1/CustomToast.java \ android/AndroidManifest.xml
}
and my CustomToast.java contains the following code
package com.sample.test1;import android.app.Activity;
import android.widget.Toast;
import org.qtproject.qt.android.QtNative;
public class CustomToast{public static void CreateSampleToast(){ Toast.makeText(QtNative.activity,"sample",Toast.LENGTH_LONG); }
}
and my main.cpp as follows
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtCore/private/qandroidextras_p.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);QQmlApplicationEngine engine; const QUrl url(u"qrc:/test1/main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); QJniObject::callStaticMethod <void>("com/sample/test1/CustomToast","CreateSampleToast","()V"); return app.exec();
}
but after building / running it on android emulator i get an exception below related on the CustomToast
W System.err: java.lang.ClassNotFoundException: Didn't find class "com.sample.test1.CustomToast" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64]]
W System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
W System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
W System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
W System.err: at org.qtproject.qt.android.QtNative.startQtApplication(Native Method)
W System.err: at org.qtproject.qt.android.QtNative$7.run(QtNative.java:658)
W System.err: at org.qtproject.qt.android.QtThread$1.run(QtThread.java:61)
W System.err: at java.lang.Thread.run(Thread.java:923)I don't know what's is wrong with my code ,
by the way I'm using the latest QT6 (6.3.1)
and the compiler is qmakei need to call the java method also i want to create my own Activity that will run first before QtActivity
but when trying the same error exception appear "ClassNotFound"please help.
Thanks
-
okay got it working , by adding new folder on android folder so the format is android/src/myapp/com/sample/test1/CustomToast.java
but now i'm getting new error
W System.err: java.lang.NullPointerException: Can't toast on a thread that has not called Looper.prepare()
W System.err: at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:157)
W System.err: at android.widget.Toast.getLooper(Toast.java:179)
W System.err: at android.widget.Toast.<init>(Toast.java:164)
W System.err: at android.widget.Toast.makeText(Toast.java:492)
W System.err: at android.widget.Toast.makeText(Toast.java:480)
W System.err: at com.sample.test1.CustomToast.CreateSampleToast(CustomToast.java:9)
W System.err: at org.qtproject.qt.android.QtNative.startQtApplication(Native Method)
W System.err: at org.qtproject.qt.android.QtNative$7.run(QtNative.java:658)
W System.err: at org.qtproject.qt.android.QtThread$1.run(QtThread.java:61)
W System.err: at java.lang.Thread.run(Thread.java:923) -