简体   繁体   中英

JNI - Alert dialog on Android

I want to show simple Alert dialog on Android with JNI. Here is my code:

myApp.cpp:

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

MainActivity:

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();
}

My application has crash whenever I want to use it and here is it's log:

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)

Any suggestion to solve it?

Use this:

    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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM