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函数

相关推荐
小老鼠不吃猫1 分钟前
力学笃行(二)Qt 示例程序运行
开发语言·qt
Ling_suu27 分钟前
SpringBoot3——Web开发
java·服务器·前端
天使day44 分钟前
SpringMVC
java·spring·java-ee
CodeClimb1 小时前
【华为OD-E卷-简单的自动曝光 100分(python、java、c++、js、c)】
java·python·华为od
风清云淡_A1 小时前
【java基础系列】实现数字的首位交换算法
java·算法
Gao_xu_sheng1 小时前
Java程序打包成exe,无Java环境也能运行
java·开发语言
大卫小东(Sheldon)1 小时前
Java的HTTP接口测试框架Gatling
java
谢家小布柔1 小时前
java中的继承
java·开发语言
l138494274511 小时前
Java每日一题(2)
java·开发语言·游戏
苹果醋32 小时前
SpringBoot快速入门
java·运维·spring boot·mysql·nginx