2015-12-30 68 views
1

我想用JNI在Android上顯示簡單的警報對話框。這裏是我的代碼:JNI - Android上的警報對話框

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

我會建議AlertDialog不能從任何地方,但主線程啓動。而你的Qt代碼不在主要的Java線程中。檢查出來:http://stackoverflow.com/questions/9990242/alert-dialog-from-thread-android – jpo38

回答

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