Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Can't create handler inside thread that has not called Looper.prepare()
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved Mobile and Embedded
2 Posts 1 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • thamT Offline
    thamT Offline
    tham
    wrote on last edited by
    #1

    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
      --android
          --src
              --com
                  --tham
                      --DetectorActivity.java
    

    DetectorActivity.java after simplify

    package com.tham;
    
    import org.qtproject.qt5.android.bindings.QtApplication;
    import org.qtproject.qt5.android.bindings.QtActivity;
    
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.util.Log;
    
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    public class DetectorActivity extends QtActivity
    {
        public DetectorActivity()
        {
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        protected void onDestroy()
        {
            super.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[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        qmlRegisterType<obj_det_filter>("MyVideoFilterLib", 1, 0, "MyVideoFilter");
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    
        new QAndroidJniObject("com/tham/DetectorActivity",
                              "()V");
    
        return app.exec();
    }
    

    Any idea why this happen?Thanks

    1 Reply Last reply
    0
    • thamT Offline
      thamT Offline
      tham
      wrote on last edited by tham
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved