JNI-安卓系统上的警报对话框

JNI - Alert dialog on Android

本文关键字:对话框 系统 JNI-      更新时间:2023-10-16

我想在带有JNI的Android上显示简单的警报对话框。这是我的代码:

myApp.cpp:

QtAndroid::androidActivity().callMethod<void>("popupDialogMain", "()V");

主要活动:

public void popupDialogMain()
{
    Log.d("Alert Dialog ", "33333333--------------------------");
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    Log.d("Alert Dialog ", "44444--------------------------");
    builder.setMessage("Look at this dialog!")
           .setCancelable(true)
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    //do things
               }
           });
    AlertDialog alert = builder.create();
    alert.show();
}

每当我想使用我的应用程序时,它就会崩溃,这是它的日志:

D/Alert Dialog (28644): 33333333--------------------------
D/Alert Dialog (28644): 44444--------------------------
F/art     (28644): art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION:id
F/art     (28644): art/runtime/check_jni.cc:65]     in call to NewGlobalRef
F/art     (28644): art/runtime/check_jni.cc:65] "QtThread" prio=10 tid=11 Runnable
F/art     (28644): art/runtime/check_jni.cc:65]   | group="main" sCount=0 dsCount=0 obj=0x12c4e440 self=0xa1433400
F/art     (28644): art/runtime/check_jni.cc:65]   | sysTid=28825 nice=-11 cgrp=apps sched=0/0 handle=0xaef7ec00
F/art     (28644): art/runtime/check_jni.cc:65]   | state=R schedstat=( 1790287538 286358026 2178 ) utm=151 stm=28 core=0 HZ=100
F/art     (28644): art/runtime/check_jni.cc:65]   | stack=0xaed4c000-0xaed4e000 stackSize=1012KB
F/art     (28644): art/runtime/check_jni.cc:65]   | held mutexes= "mutator lock"(shared held)
F/art     (28644): art/runtime/check_jni.cc:65]   native: #00 pc 00004640  /system/lib/libbacktrace_libc++.so (UnwindCurrent::Unwind(unsigned int, ucontext*)+23)

有什么解决的建议吗?

使用此:

    public void popupDialogMain()
    {
        Log.d("Alert Dialog ", "33333333--------------------------");
        final Context context = getApplicationContext();
        Handler h1 = new Handler(context.getMainLooper());
        h1.post(new Runnable() {
            @Override
            public void run() {
                popupDialogMain1();
            }
        });
    }
    public void popupDialogMain1()
    {
        Log.d("Alert Dialog ", "44444--------------------------");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        Log.d("Alert Dialog ", "5555--------------------------");
        builder.setMessage("Look at this dialog!")
               .setCancelable(true)
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        //do things
                   }
               });
        AlertDialog alert = builder.create();
        alert.show();
    }