Qt 6 Android No static method for Toast?
Solved
Qt 6
-
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:
-
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"; }
-