Java Constructor not getting called while using QAndroidJNIObject

  • I am working on modifying the Flickr application demonstrated by Jens Bache-Wiig at "Qt Developer Days in Munich in 2011":

    I am trying to share the image(Experimenting with plain text first) using ACTION_SEND Intent. My calls are passing through from QML to C++ and it also passes the Java function. However, it fails because the java code can't find the instance of the QtActivity (From the java file below, I only get the log from m_instance== null. The log for the constructor is never printed.) I have tried to compare my code with the NotificationManager example and I am not able to find anything that would prevent the java constructor from being called.

    What am I missing or doing wrong?

    QT Class file
    @#include "androidfunctions.h"
    #include <QtAndroidExtras>
    #include <QDebug>

    AndroidFunctions::AndroidFunctions(QObject *parent) :
    connect(this, SIGNAL(ShareContentChanged()), this, SLOT(shareContentToAndroid()));

    void AndroidFunctions::setShareContent(const QString &ShareContent)
    if(m_sharecontent == ShareContent){

    qDebug() << "ShareContent set";
    m_sharecontent = ShareContent;
    emit ShareContentChanged();


    QString AndroidFunctions::ShareContent() const
    qDebug() << "ShareContent read";
    return m_sharecontent;

    void AndroidFunctions::shareContentToAndroid()
    qDebug() << "Sharing Content to AndroidFunctions";
    QAndroidJniObject javaShareContent = QAndroidJniObject::fromString(m_sharecontent);
    QAndroidJniObject javaMimeType = QAndroidJniObject::fromString(m_mimetype);
    qDebug() << "Declaring AndroidFunctions";
    QAndroidJniObject andfunc("uk/me/samaria/flickshare/AndroidFuctions");


    Java Class

    import android.content.Intent;
    import android.content.Context;
    import android.util.Log;
    import android.os.Bundle;

    public class AndroidFunctions extends
    private static final String TAG = "FLICKR_SHARE: ";
    private static AndroidFunctions m_instance;
    private Context context;
    private Intent intent;
    public AndroidFunctions()
    Log.i(TAG, "Android Functions Instantiated");
    m_instance = this;

    public  static void shareText(final String mimetype, final String mimetext)
        if(m_instance == null) {
            Log.e(TAG, "text sharing failed");
        try {
            Log.e(TAG, "text sharing started");
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT, "");
            Log.e(TAG, "text sharing activity starting");
            m_instance.startActivity(Intent.createChooser(intent, "Share with"));
            Log.e(TAG, "text sharing activity started");
        } catch (Exception e){
            Log.e(TAG, "Exception caught when sharing text!", e);


    @#include <QApplication>
    #include <QtQuick>
    #include <QSysInfo>
    #include "androidfunctions.h"

    int main(int argc, char *argv[])
    QApplication app(argc, argv);

    const QString mainQmlApp = QLatin1String("qrc:///main.qml");
    QQuickView view;
    AndroidFunctions *androidfunctions = new AndroidFunctions(&view);
    view.engine()->rootContext()->setContextProperty(QLatin1String("androidfunctions"), androidfunctions);
    QObject::connect(view.engine(), SIGNAL(quit()), qApp, SLOT(quit()));

    // view.setGeometry(QRect(100, 100, 360, 640));;
    return app.exec();


  • Finally the manifest:
    <?xml version="1.0"?>
    <manifest package="" xmlns:android="" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
    <application android:hardwareAccelerated="true" android:name="" android:label="@string/app_name">
    <activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="" android:label="@string/app_name" android:screenOrientation="unspecified" android:launchMode="singleTop">
    <action android:name="android.intent.action.EDIT"/>
    <action android:name="android.intent.action.VIEW"/>
    <action android:name="android.intent.action.SEND"/>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    <data android:mimeType="text/plain" />

            &lt;meta-data android:name="" android:value="-- %%INSERT_APP_LIB_NAME%% --"/&gt;
            &lt;meta-data android:name="" android:resource="@array/qt_sources"/&gt;
            &lt;meta-data android:name="" android:value="default"/&gt;
            &lt;meta-data android:name="" android:resource="@array/qt_libs"/&gt;
            &lt;meta-data android:name="" android:resource="@array/bundled_libs"/&gt;
            &lt;!-- Deploy Qt libs as part of package --&gt;
            &lt;meta-data android:name="" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/&gt;
            &lt;meta-data android:name="" android:resource="@array/bundled_in_lib"/&gt;
            &lt;meta-data android:name="" android:resource="@array/bundled_in_assets"/&gt;
            &lt;!-- Run with local libs --&gt;
            &lt;meta-data android:name="" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/&gt;
            &lt;meta-data android:name="" android:value="/data/local/tmp/qt/"/&gt;
            &lt;meta-data android:name="" android:value="-- %%INSERT_LOCAL_LIBS%% --"/&gt;
            &lt;meta-data android:name="" android:value="-- %%INSERT_LOCAL_JARS%% --"/&gt;
            &lt;meta-data android:name="" android:value="-- %%INSERT_INIT_CLASSES%% --"/&gt;
            &lt;!--  Messages maps --&gt;
            &lt;meta-data android:value="@string/ministro_not_found_msg" android:name=""/&gt;
            &lt;meta-data android:value="@string/ministro_needed_msg" android:name=""/&gt;
            &lt;meta-data android:value="@string/fatal_error_msg" android:name=""/&gt;
            &lt;!--  Messages maps --&gt;
            &lt;!-- Splash screen --&gt;
            &lt;meta-data android:name="" android:resource="@drawable/logo"/&gt;
            &lt;!-- Splash screen --&gt;
    <uses-sdk android:minSdkVersion="18"/>
    <supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
    &lt;!-- The following comment will be replaced upon deployment with default permissions based on the dependencies of the application.
         Remove the comment if you do not require these default permissions. --&gt;
    &lt;!-- %%INSERT_PERMISSIONS --&gt;
    &lt;!-- The following comment will be replaced upon deployment with default features based on the dependencies of the application.
         Remove the comment if you do not require these default features. --&gt;
    &lt;!-- %%INSERT_FEATURES --&gt;


  • Found the solution to my problem.

    The android:name in the <activity> section of the manifest should be changed to my java Class. instead of

    Now that, I am able to share text, onward to share an Image and see if that works.

Log in to reply

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