JNI - Android

时间:2015-12-30 07:49:17

标签: java android c++ qt java-native-interface

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

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

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

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)

有什么建议可以解决吗?

1 个答案:

答案 0 :(得分:0)

使用此:

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