Can't create handler inside thread that has not called Looper.prepare()

  • I would like to load create a class of java in the c++ classes. But it always throw the exception "JNI DETECTED ERROR IN APPLICATION: JNI NewString called with pending exception java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()"

    Android api : 24~28
    NDK : 18b
    JDK: jdk1.8.0_201
    build.gradle: I add

    compileOptions {
            sourceCompatibility = '1.8'
            targetCompatibility = '1.8'

    into the android block and the dependencies of tensorflowlite as stackoverflow show.

    My layout

    Source folders
 after simplify

    package com.tham;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.util.Log;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    public class DetectorActivity extends QtActivity
        public DetectorActivity()
        public void onCreate(Bundle savedInstanceState)
        protected void onDestroy()

    I tried to create this java object in the subclass of QVideoFilterRunnable and the main funciton, but both of them give me the same error messages. Codes of main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QtQml>
    #include <QtAndroidExtras>
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        qmlRegisterType<obj_det_filter>("MyVideoFilterLib", 1, 0, "MyVideoFilter");
        new QAndroidJniObject("com/tham/DetectorActivity",
        return app.exec();

    Any idea why this happen?Thanks

  • I think I find out the reason, if the java object is a subclass of QtActivity, it need to run in gui thread(call this in android communities?), if you do not want to bother with this issue, do not make your class as the subclass of QtActivity.

Log in to reply