[Android-development] QTimer crashes by JNI-call (native Android) with Errors

Maik Ziemert mail at moozoom.de
Sun Jun 12 12:21:19 CEST 2016


Hello,


I have a big problem with my Qt library - integrated in a native Android App over JNI.

I wrote a static qt library. In this library I instantiate a QCoreApplication and a QTimer:


@QT CODE

void MYQSimpleLibrary::createCoreApplication()
{   
    int argc = 1;
    char * argv[] = {"my.app", NULL};

    if (QCoreApplication::instance() == NULL)
    {
        new QCoreApplication(argc, argv);
    }

    return;
}

int MYQSimpleLibrary::exec()
{
    return QCoreApplication::exec();
}

void MYQSimpleLibrary::createTimer()
{
    QTimer *requestTimer = new QTimer(this);

    requestTimer->start(1000);

    connect(requestTimer, SIGNAL(timeout()), this, SLOT(tick()));

    return;
}

void MYQSimpleLibrary::tick()
{
    qDebug() << QThread::currentThreadId() << " tick()";

    return;
}

@QT CODE END


I know, this timer needs an event loop.

In my android project I will integrate this qt static library over jni:


@JNI CODE

#include <string.h>
#include <jni.h>
#include <android/log.h>

#include <qtlibrary.h>

static QtLibrary qtLib;

extern "C" {
	JNIEXPORT void JNICALL Java_com_company_qtlibrary_Interface_test(JNIEnv * env, jobject obj);
};

JNIEXPORT void JNICALL Java_com_company_qtlibrary_Interface_test(JNIEnv * env, jobject obj)
{
	qtLib.createCoreApplication();
	qtLib.createTimer();
	qtLib.exec();

	return;
}

@JNI CODE END


In my android app I call this test method with following code:


@JNI CODE

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		System.out.println("Call test() from lib:");
		
		new Thread(new Runnable() {
		 @Override
		 public void run() {
	 
			Looper.prepare();
			
			try{
				System.out.println("test() is running...");
				Interface myInterface = new Interface();
				myInterface.test();
			}catch(Exception e){
				System.out.println("ERROR: " + e.toString());
			}

		 }
		}).start();		
	}
}

@JNI CODE END


If I let running my android app, while the qt call over JNI, the app crashes:

06-12 12:08:10.501: W/(9663): (null):0 ((null)): WARNING: QApplication was not created in the main() thread.

06-12 12:08:10.501: W/my.app(9663): (null):0 ((null)): QObject: Cannot create children for a parent that is in a different thread.
06-12 12:08:10.501: W/my.app(9663): (Parent is MYQSimpleLibrary(0x6075e010), parent's thread is QThread(0x5cc43e28), current thread is QThread(0x5cc44810)
06-12 12:08:10.501: W/my.app(9663): (Parent is MYQSimpleLibrary(0x6075e010), parent's thread is QThread(0x5cc43e28), current thread is QThread(0x5cc44810)


How can I solve this problem? If I call this native method in the main thread, the android gui is freezing:


@JNI CODE

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		System.out.println("Call test() from lib:");
		
		Interface myInterface = new Interface();
		myInterface.test();	
	}
}

@JNI CODE END


Please help me in this case. I dont know what I can do to find a good solution.


Thank you in advanced!


More information about the Android-development mailing list