Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Qt 6 Android No static method for Toast?

Qt 6 Android No static method for Toast?

Scheduled Pinned Locked Moved Solved Qt 6
android jniqt6android javarunonuithreadmobile
2 Posts 1 Posters 471 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.
  • G Offline
    G Offline
    gween
    wrote on last edited by
    #1

    What am I doing wrong in my function when trying to display this Toast?

    QNativeInterface::QAndroidApplication::runOnAndroidMainThread([=]() {
                        QJniEnvironment env;
                        jclass clazz = env.findClass("android/widget/Toast");
                        QJniObject displayText = QJniObject::fromString("Hello world");
                        jint timeout = 10;
                        QJniObject callableToast = QJniObject::callStaticMethod<jobject>(
                            clazz,
                            "makeText",
                            QNativeInterface::QAndroidApplication::context(),
                            displayText.object<jstring>(),
                            timeout
                        );
    
                        callableToast.callMethod<void>("show");
                                         QNativeInterface::QAndroidApplication::context());
        }).waitForFinished();
    

    This keeps failing with:

    java.lang.NoSuchMethodError: no static method "Landroid/widget/Toast;.makeText(Landroid/content/Context;Ljava/lang/String;I)Ljava/lang/Object;"
    

    The args and return type seem correct to me based on Android Toast docs. I've also tried changing the return type to jclass which fails with a similar error.

    FWIW, I've also tried explicitly setting the signature, and it fails with a similar error:

    QJniObject callableToast = QJniObject::callStaticMethod<jobject>(
                            clazz,
                            "makeText",
    // "Landroid/content/Context;Ljava/lang/String;I)Landroid/widget/Toast;"
                            "(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast",
                            QNativeInterface::QAndroidApplication::context(),
                            displayText.object<jstring>(),
                            timeout
                        );
    

    I also see this in the logs, but don't know if it is valuable:

    W System  : ClassLoader referenced unknown path:
    
    1 Reply Last reply
    0
    • G Offline
      G Offline
      gween
      wrote on last edited by
      #2

      For the future generations of humanity:

      QJniObject toast = QJniObject::callStaticObjectMethod(
                              "android/widget/Toast",
                              "makeText",
                              "(Landroid/content/Context;Ljava/lang/CharSequence;I)"
                              "Landroid/widget/Toast;",
                              QNativeInterface::QAndroidApplication::context(),
                              displayText.object<jstring>(),
                              timeout
                          );
      
                          if (toast.isValid()) {
                              qDebug() << "Toast is valid";
                              toast.callMethod<void>("show");
                          } else {
                              qDebug() << "Toast JNI object not valid";
                          }
      
      1 Reply Last reply
      0
      • G gween has marked this topic as solved on

      • Login

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