Solved 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 addcompileOptions { 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
-
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.