Qt Android Native Error: JNI DETECTED ERROR IN APPLICATION: java_object == null

开发的qt android程序在低版本上运行正常,在高版本上启动时崩溃,报如下错误

W  java.lang.RuntimeException: Can't create handler inside thread Thread[qtMainLoopThread,5,main] that has not called Looper.prepare()
   	at android.os.Handler.<init>(Handler.java:227)
   	at android.os.Handler.<init>(Handler.java:129)
   	at android.app.Activity.<init>(Activity.java:980)
   	at org.qtproject.qt.android.bindings.QtActivity.<init>(QtActivity.java:67)
   	at cn.pubwell.cons.ConsQtActivity.<init>(ConsQtActivity.java:58)
   	at org.qtproject.qt.android.QtNative.startQtApplication(Native Method)
   	at org.qtproject.qt.android.QtNative$7.run(QtNative.java:465)
   	at org.qtproject.qt.android.QtThread$1.run(QtThread.java:25)
   	at java.lang.Thread.run(Thread.java:1012)
A  java_vm_ext.cc:591] JNI DETECTED ERROR IN APPLICATION: java_object == null
   java_vm_ext.cc:591]     in call to GetObjectClass
   java_vm_ext.cc:591]     from void org.qtproject.qt.android.QtNative.startQtApplication()
D  Compat change id reported: 171228096; UID 10317; state: ENABLED
D  Compat change id reported: 193247900; UID 10317; state: DISABLED
A  runtime.cc:691] Runtime aborting...
   runtime.cc:691] Dumping all threads without mutator lock held
   runtime.cc:691] All threads:
   runtime.cc:691] DALVIK THREADS (42):
   runtime.cc:691] "main" prio=10 tid=1 Runnable
   runtime.cc:691]   | group="" sCount=0 ucsCount=0 flags=0 obj=0x71d226c0 self=0xb4000072732e8c00
   runtime.cc:691]   | sysTid=22682 nice=-10 cgrp=top-app sched=0/0 handle=0x72749274f8
   runtime.cc:691]   | state=R schedstat=( 613865508 70633029 567 ) utm=50 stm=11 core=6 HZ=100
   runtime.cc:691]   | stack=0x7fd5ccc000-0x7fd5cce000 stackSize=8188KB
   runtime.cc:691]   | held mutexes= "mutator lock"(shared held)
   runtime.cc:691]   native: #00 pc 0056f570  /apex/com.android.art/lib64/libart.so (art::DumpNativeStack+176) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #01 pc 0068ffa8  /apex/com.android.art/lib64/libart.so (art::Thread::DumpStack const+344) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #02 pc 006ae5a8  /apex/com.android.art/lib64/libart.so (art::DumpCheckpoint::Run+920) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #03 pc 00690f0c  /apex/com.android.art/lib64/libart.so (art::Thread::RunCheckpointFunction+188) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #04 pc 002a4534  /apex/com.android.art/lib64/libart.so (art::Thread::CheckSuspend+64) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #05 pc 00286b74  /apex/com.android.art/lib64/libart.so (art::ClassLinker::InitializeClass+1128) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #06 pc 00266518  /apex/com.android.art/lib64/libart.so (art::ClassLinker::EnsureInitialized+164) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #07 pc 00236018  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>+35964) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #08 pc 00229bd8  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #09 pc 0014dd26  /data/dalvik-cache/arm64/product@app@WebViewGoogle64@WebViewGoogle64.apk@classes.vdex (com.android.webview.chromium.WebViewChromiumFactoryProvider.addWebViewAssetPath)
   runtime.cc:691]   native: #10 pc 003db018  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +452) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #11 pc 003e19b8  /apex/com.android.art/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge+104) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #12 pc 003e2538  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>+1760) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #13 pc 00230a58  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>+14012) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #14 pc 00229bd8  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #15 pc 00150408  /data/dalvik-cache/arm64/product@app@WebViewGoogle64@WebViewGoogle64.apk@classes.vdex (com.android.webview.chromium.WebViewChromium.<init>)
   runtime.cc:691]   native: #16 pc 003db018  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +452) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #17 pc 003e19b8  /apex/com.android.art/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge+104) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #18 pc 003e2538  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>+1760) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)
   runtime.cc:691]   native: #19 pc 00230a58  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>+14012) (BuildId: 43f5b252fbfc41f385c66b6a496e3d08)

网络上没有查找到有用信息,最终通过二分法找到了错误代码

...
QJniEnvironment env;
QJniObject javaClass("cn/pubwell/cons/ConsQtActivity"); // 使用findclass似乎也有同样的问题
jobject objAct = qObjAct.object<jobject>();
jclass objectClass = env->GetObjectClass( objAct );
...
env->RegisterNatives(objectClass,
                         methods,
                         sizeof(methods) / sizeof(methods[0]));
...

上面的注册方式有问题,应当使用如下方式

    ...
    QJniEnvironment env;
    jclass objectClass = env->GetObjectClass(javaClass.object<jobject>());
    QJniObject qObjAct = QJniObject::callStaticObjectMethod(             
                                                          "com/example/ExampleActivity",
                                                            "getContext",
                                                            "()Landroid/content/Context;");

    if (!qObjAct.isValid())
    {
        return ;
    }

    jobject objAct = qObjAct.object<jobject>();
    jclass objectClass = env->GetObjectClass( objAct );

    env->RegisterNatives(objectClass,
                         methods,
                         sizeof(methods) / sizeof(methods[0]));
    ...

其中getContext为com/example/ExampleActivity.java的静态获取context函数

相关推荐
苏-言4 分钟前
Spring IOC实战指南:从零到一的构建过程
java·数据库·spring
界面开发小八哥12 分钟前
更高效的Java 23开发,IntelliJ IDEA助力全面升级
java·开发语言·ide·intellij-idea·开发工具
草莓base25 分钟前
【手写一个spring】spring源码的简单实现--容器启动
java·后端·spring
Dnelic-29 分钟前
【单元测试】【Android】JUnit 4 和 JUnit 5 的差异记录
android·junit·单元测试·android studio·自学笔记
Allen Bright38 分钟前
maven概述
java·maven
编程重生之路40 分钟前
Springboot启动异常 错误: 找不到或无法加载主类 xxx.Application异常
java·spring boot·后端
薯条不要番茄酱41 分钟前
数据结构-8.Java. 七大排序算法(中篇)
java·开发语言·数据结构·后端·算法·排序算法·intellij-idea
努力进修1 小时前
“探索Java List的无限可能:从基础到高级应用“
java·开发语言·list
politeboy1 小时前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
Daniel 大东2 小时前
BugJson因为json格式问题OOM怎么办
java·安全