Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [HELP] QT6 calling android java from c++
QtWS25 Last Chance

[HELP] QT6 calling android java from c++

Scheduled Pinned Locked Moved Solved Language Bindings
android javac++bindingscalling method
4 Posts 2 Posters 1.9k Views
  • 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.
  • J Offline
    J Offline
    jhayar
    wrote on last edited by jhayar
    #1

    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.user

    my test1.pro contains the following code
    QT += quick

    SOURCES +=
    main.cpp

    resources.files = main.qml
    resources.prefix = /$${TARGET}
    RESOURCES += resources

    Additional 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 += target

    android {

    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 qmake

    i 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

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jhayar
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jhayar
        wrote on last edited by
        #3

        okay got it working , this will mark as solved thanks

        N 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD referenced this topic on
        • J jhayar

          okay got it working , this will mark as solved thanks

          N Offline
          N Offline
          nikich340
          wrote on last edited by
          #4

          @jhayar Could you please describe your solution to the "Can't toast on a thread that has not called Looper.prepare()"?

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved