[SOLVED] [Android] [Qt 5.2] How to get activity object?

  • I need to get main activity, and I tried:
    QAndroidJniEnvironment jniEnv;
    jclass clsAct = jniEnv->FindClass("android/app/Activity");
    qDebug() << "Activity: " << clsAct;

    if( QAndroidJniObject::isClassAvailable("org/qtproject/qt5/android/bindings/QtActivity") )
    jclass clsApp = jniEnv->FindClass("org/qtproject/qt5/android/bindings/QtActivity");
    qDebug() << "QtActivity: " << clsApp;

    if( clsApp && clsAct )
        jobject objAct = NULL;
        jmethodID mid = jniEnv->GetStaticMethodID( clsApp, "activity", "()Landroid/app/Activity;" );
        qDebug() << "activityMID: " << mid;
        if( mid )
            objAct = jniEnv->CallStaticObjectMethod( clsApp, mid );
            qDebug() << "activityObj: " << objAct;

    But on jniEnv->FindClass("org/qtproject/qt5/android/bindings/QtActivity") shoots error:
    D/Qt ( 4074): mainwindow.cpp:32 (MainWindow::MainWindow(QWidget*)): >>>>>> QtActivity: 0x0
    W/dalvikvm( 4074): JNI WARNING: JNI function CallStaticVoidMethodV called with exception pending
    W/dalvikvm( 4074): in Ldalvik/system/NativeStart;.run:()V (CallStaticVoidMethodV)
    W/dalvikvm( 4074): Pending exception is:
    I/dalvikvm( 4074): java.lang.NoClassDefFoundError: org/qtproject/qt5/android/bindings/QtActivity
    I/dalvikvm( 4074): at dalvik.system.NativeStart.run(Native Method)
    I/dalvikvm( 4074): Caused by:
    I/dalvikvm( 4074): java.lang.ClassNotFoundException: Didn't find class "org.qtproject.qt5.android.bindings.QtActivity" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib]]

  • I couldn't use this approach, but what ever you want you can try to edit the generated java code

  • Qt Champions 2016

    Try this piece of code:

    @ QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");

  • I've looked to a lot of posts explaining how to sort this problem, but the only one thing that worked was this:

    I was in QT 5.2:


    in your .pro:
    @QT += gui-private@

    now in your cpp:

    #include <QAndroidJniObject>
    #include <qpa/qplatformnativeinterface.h>

    QAndroidJniObject getMainActivity()
    QPlatformNativeInterface *interface = QApplication::platformNativeInterface();
    QAndroidJniObject activity = (jobject)interface->nativeResourceForIntegration("QtActivity");

    qDebug()<<"CLASS NOT VALID!!!!!!!!";

    return activity;

    I also like to thanks the MontezooM post which helped me to figure out

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.