Can’t create java class by QAndroidJniObject

1.3k views Asked by At

Try to create a java class from c++ side but keep crashing

appBucksAdv.java

package adv;

import org.qtproject.qt5.android.bindings.QtApplication;
import org.qtproject.qt5.android.bindings.QtActivity;

import android.os.Bundle;

public class appBucksAdv extends QtActivity
{
}

simpleJNI.hpp

#include <QObject>
#include <QtAndroidExtras/QAndroidJniObject>

class simpleJNI : public QObject
{
Q_OBJECT
public:
explicit simpleJNI(QObject *parent = nullptr);

private:
QAndroidJniObject advClass_;

};

simpleJNI.cpp

#include "simpleJNI.hpp"

simpleJNI::simpleJNI(QObject *parent) :
QObject(parent),
advClass_("adv/appBucksAdv") //crash when I create the java class
{
}

error messages : # W/dalvikvm(21397): dvmFindClassByName rejecting 'org/qtproject/qt5/android/QtMessageDialogHelper'

# D/dalvikvm(21397): Trying to load lib /data/data/org.qtproject.example.androidTest/lib/libQt5QuickParticles.so 0x4176efd0

# D/dalvikvm(21397): Shared lib '/data/data/org.qtproject.example.androidTest/lib/libQt5QuickParticles.so' already loaded in same CL 0x4176efd0

# D/dalvikvm(21397): Trying to load lib /data/data/org.qtproject.example.androidTest/lib/libandroidTest.so 0x4176efd0

# D/dalvikvm(21397): Added shared lib /data/data/org.qtproject.example.androidTest/lib/libandroidTest.so 0x4176efd0

# D/dalvikvm(21397): No JNI_OnLoad found in /data/data/org.qtproject.example.androidTest/lib/libandroidTest.so 0x4176efd0, skipping init

# W/Qt (21397): kernel/qcoreapplication.cpp:416 (QCoreApplicationPrivate::QCoreApplicationPrivate(int&, char**, uint)): WARNING: QApplication was not created in the main() thread.

# W/dalvikvm(21397): dvmFindClassByName rejecting 'org/qtproject/qt5/android/QtNativeInputConnection'

# W/dalvikvm(21397): dvmFindClassByName rejecting 'org/qtproject/qt5/android/QtExtractedText'

1.full messages

2.codes

1

There are 1 answers

0
cstream On BEST ANSWER

Are you loading the correct Activity? you need to edit the manifest to make it load your custom activity. You should also avoid calling the constructor of the QtActivity class yourself (advClass_("adv/appBucksAdv")), as it will attempt to prepare the application again. What you probably want is a handle to the activity and since you are already extending the QtActivity class, just add a static function that returns a handle to it.

E.g.:

Java:

public class appBucksAdv extends QtActivity
{
 ...
 static appBucksAdv getActivity() { return thisActivity; }
 ...
}

C++

QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("adv/appBucksAdv",
                                                                       "getActivity",
                                                                       "()Ladv/appBucksAdv;");