2016-08-16 13 views
0

これについて多くの質問がありましたが、私はすでにこの種のエラーを扱っていますが、私はJNIの特別なコンテストに参加しています。だからここ Android JNI - Call AttachCurrentThread without DetachCurrentThreadNNNのポインタの例外JNIからのダイアログビルダの呼び出し

は、私が何をすべきかです: 私は、別のC++のスレッドがいくつかのコードを実行しているし、(タイマーが終了した後に)いくつかの点で私はJavaでコールバック関数を呼び出す必要があります。このコールバック関数は、いくつかのアラートを表示してから、いくつかのコードを実行するために使用されます。

JNIEXPORT void JNICALL package_endTrialJava(){ 

    LOGD("EndTrialJava"); 
    JNIEnv* env; 

    int getEnvStat = g_jvm->GetEnv((void **)&env, JNI_VERSION_1_6); 
    if (getEnvStat == JNI_EDETACHED) { 
     LOGD("GetEnv: not attached"); 
     //if (g_jvm->AttachCurrentThread((void **) &env, NULL) != 0) { 
     if (g_jvm->AttachCurrentThread(&env, NULL) != 0) { 
      LOGD("Failed to attach"); 
     } 
     else{ 
      jobject javaObjectRef = env->NewObject(javaClassRef, javaMethodRef); 
      env->CallVoidMethod(javaObjectRef, javaMethodRef); 
     } 
    } else if (getEnvStat == JNI_OK) { 
     // 
    } else if (getEnvStat == JNI_EVERSION) { 
     LOGD("GetEnv: version not supported"); 
    } 

    //See https://stackoverflow.com/questions/26534304/android-jni-call-attachcurrentthread-without-detachcurrentthread 
    g_jvm->DetachCurrentThread(); 
} 

そしてJavaのサイドに呼び出される関数は、以下のん:

だから基本的にここに私のコードC++側だ AlertDialog.Builder警告=新しいAlertDialog.Builder(MainActivity.this)は、 alert.setTitle( "次のテクニック"); alert.setMessage( "他の方法を試します");

W/art  (24708): Thread[20,tid=24780,Native,Thread*=0x4e1ab1c8,peer=0x649247a0,"Thread-1128"] attached without supplying a name 
D/TEST (24708): End Trial 
F/art  (24708): art/runtime/check_jni.cc:64] JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV called with pending exception 'java.lang.NullPointerException' thrown in unknown throw location 
F/art  (24708): art/runtime/check_jni.cc:64]  in call to CallVoidMethodV 
F/art  (24708): art/runtime/check_jni.cc:64] "Thread-1128" prio=5 tid=20 Runnable 
F/art  (24708): art/runtime/check_jni.cc:64] | group="main" sCount=0 dsCount=0 obj=0x649247a0 self=0x4e1ab1c8 
F/art  (24708): art/runtime/check_jni.cc:64] | sysTid=24780 nice=0 cgrp=apps sched=0/0 handle=0x493da480 
F/art  (24708): art/runtime/check_jni.cc:64] | state=R schedstat=(282584 1729083 7) utm=0 stm=0 core=3 HZ=100 
F/art  (24708): art/runtime/check_jni.cc:64] | stack=0x4e9cf000-0x4e9d3000 stackSize=1016KB 
F/art  (24708): art/runtime/check_jni.cc:64] (no managed stack frames) 
F/art  (24708): art/runtime/check_jni.cc:64] 
F/art  (24708): art/runtime/runtime.cc:203] Runtime aborting... 
F/art  (24708): art/runtime/runtime.cc:203] Aborting thread: 
F/art  (24708): art/runtime/runtime.cc:203] "Thread-1128" prio=5 tid=20 Runnable 
F/art  (24708): art/runtime/runtime.cc:203] | group="main" sCount=0 dsCount=0 obj=0x649247a0 self=0x4e1ab1c8 
F/art  (24708): art/runtime/runtime.cc:203] | sysTid=24780 nice=0 cgrp=apps sched=0/0 handle=0x493da480 
F/art  (24708): art/runtime/runtime.cc:203] | state=R schedstat=(1254668 1729083 7) utm=0 stm=0 core=3 HZ=100 
F/art  (24708): art/runtime/runtime.cc:203] | stack=0x4e9cf000-0x4e9d3000 stackSize=1016KB 
F/art  (24708): art/runtime/runtime.cc:203] native: art::Thread::DumpStack(std::ostream&) const+87 [0x41709b78] (libart.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: ??? [0x41701c62] (libart.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: art::Runtime::Abort()+79 [0x41702204] (libart.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: art::LogMessage::~LogMessage()+507 [0x415ad89c] (libart.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: ??? [0x415b5ad4] (libart.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: art::JniAbortF(char const*, char const*, ...)+51 [0x415b6254] (libart.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: ??? [0x415b817c] (libart.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: ??? [0x415b83e6] (libart.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: ??? [0x415befe6] (libart.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: _JNIEnv::CallVoidMethod(_jobject*, _jmethodID*, ...)+40 [0x48c8ac68] (libnative.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: package_endTrialJava+268 [0x48c8f8f0] (libnative.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: FluidMechanics::Impl::endTrial()+772 [0x48c8fc20] (libnative.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: std::thread::_Impl<std::_Bind_simple<std::_Mem_fn<void (FluidMechanics::Impl::*)()> (FluidMechanics::Impl*)> >::_M_run()+36 [0x48c8a128] (libnative.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: execute_native_thread_routine+11 [0x47f2c53c] (libgnustl_shared.so) 
F/art  (24708): art/runtime/runtime.cc:203] (no managed stack frames) 
F/art  (24708): art/runtime/runtime.cc:203] Pending exception java.lang.NullPointerException thrown by 'unknown throw location' 
F/art  (24708): art/runtime/runtime.cc:203] java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference 
F/art  (24708): art/runtime/runtime.cc:203] at android.content.pm.ApplicationInfo android.content.ContextWrapper.getApplicationInfo() (ContextWrapper.java:152) 
F/art  (24708): art/runtime/runtime.cc:203] at android.content.res.Resources$Theme android.view.ContextThemeWrapper.getTheme() (ContextThemeWrapper.java:103) 
F/art  (24708): art/runtime/runtime.cc:203] at int android.app.AlertDialog.resolveDialogTheme(android.content.Context, int) (AlertDialog.java:143) 
F/art  (24708): art/runtime/runtime.cc:203] at void android.app.AlertDialog$Builder.<init>(android.content.Context) (AlertDialog.java:360) 
F/art  (24708): art/runtime/runtime.cc:203] at void fr.limsi.ARViewer.MainActivity.alertBeforeNewTechnique() (MainActivity.java:329) 
F/art  (24708): art/runtime/runtime.cc:203] at void fr.limsi.ARViewer.MainActivity.showAlerts() (MainActivity.java:320) 
F/art  (24708): art/runtime/runtime.cc:203] at void fr.limsi.ARViewer.MainActivity.endTrial() (MainActivity.java:369) 
F/art  (24708): art/runtime/runtime.cc:203] Dumping all threads without appropriate locks held: thread list lock mutator lock 
F/art  (24708): art/runtime/runtime.cc:203] All threads: 
F/art  (24708): art/runtime/runtime.cc:203] DALVIK THREADS (20): 
F/art  (24708): art/runtime/runtime.cc:203] "main" prio=5 tid=1 Native 
F/art  (24708): art/runtime/runtime.cc:203] | group="main" sCount=0 dsCount=0 obj=0x646c7b70 self=0x41da3c80 
F/art  (24708): art/runtime/runtime.cc:203] | sysTid=24708 nice=0 cgrp=apps sched=0/0 handle=0x4010b154 
F/art  (24708): art/runtime/runtime.cc:203] | state=S schedstat=(2797045067 1500623419 9968) utm=213 stm=66 core=0 HZ=100 
F/art  (24708): art/runtime/runtime.cc:203] | stack=0xbe552000-0xbe556000 stackSize=8MB 
F/art  (24708): art/runtime/runtime.cc:203] native: android::Looper::pollOnce(int, int*, int*, void**)+92 [0x401ab94d] (libutils.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: android::NativeMessageQueue::pollOnce(_JNIEnv*, int)+22 [0x40254331] (libandroid_runtime.so) 
F/art  (24708): art/runtime/runtime.cc:203] native: oatexec+18045636 [0x634efac4] ([email protected]@boot.oat) 
F/art  (24708): art/runtime/runtime.cc:203] at android.os.MessageQueue.nativePollOnce(Native method) 
F/art  (24708): art/runtime/runtime.cc:203] at android.os.MessageQueue.next(MessageQueue.java:138) 
F/art  (24708): art/runtime/runtime.cc:203] at android.os.Looper.loop(Looper.java:123) 
F/art  (24708): art/runtime/runtime.cc:203] at android.app.ActivityThread.main(ActivityThread.java:5030) 
F/art  (24708): art/runtime/runtime.cc:203] at java.lang.reflect.Method.invoke(Native method) 
F/art  (24708): art/runtime/runtime.cc:203] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
F/art  (24708): art/runtime/runtime.cc:203] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 

私の側からの推測は、これがUIThreadに関係しているということです。

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     alertBeforeTrial(); 
     return ; 

     } 
    }); 
    AlertDialog alertdialog = alert.create(); 
    alertdialog.show(); 

は、しかし、私は次のエラーを取得します。私のスレッドからC++のJava関数への呼び出しはUIThreadにはないので、エラーが出るのです。しかし、私はそれが実際に問題であるとは確信していないと私はそれを解決する方法について私は考えていない。あなたの誰もが私ができることを知っているだろうか?

編集:AlertDialogを扱うすべてのコードを削除しようとしましたが、問題は発生しません。だから、それはUIThreadの問題か、AlertDialogの問題かのようです...しかし、私は、この関数の外で単純なAlertDialogを作成することがUIThreadの問題であると推測しています。

+0

問題はおそらくThreadUIの問題だと思うので、Javaでダミーメソッド(右のスレッドでは1回、右のスレッドでは1回)を使用して問題を再現するかどうかを確認できますJNI。それは問題を解決することはできませんが、潜在的な問題の数を調べることを制限する可能性があります。 –

+0

@callmeSteveので、AlertDialogを扱うすべてのコードを削除しようとしましたが、問題は発生しません。だから、それはUIThreadの問題か、AlertDialogの問題かのようです...しかし、私は、この関数の外で単純なAlertDialogを作成することがUIThreadの問題であると推測しています。 – LBes

+0

それであなた自身が新しいアクティビティインスタンスを作成し、そこにメソッドを呼び出すのですか?これはうまくいきません - ダイアログのアクティビティのコンテキストなど、アクティビティが必要なものについてアクティビティは初期化されません。 – laalto

答えて

2

NewObjectコールに問題があります。

NewObjectに渡される2番目のパラメータは、コンストラクタへの参照であると考えられますが、あなたの場合はCallVoidMethodと同じメソッドです。

Constructs a new Java object. The method ID indicates which constructor method to invoke. This ID must be obtained by calling GetMethodID() with <init> as the method name and void (V) as the return type.

あなたはjavaMethodRefを初期化するので、それはそれは、コンストラクタまたは別の方法のかどうかはっきりしないコードを示していません。それがコンストラクタの場合は、一度呼び出すだけで済み、CallVoidMethod呼び出しは冗長です。それがコンストラクタでない場合は、代わりにコンストラクタを呼び出す必要があります。あなたのコンストラクタは引数を取る場合、

jmethodID constructor = env->GetMethodID(javaClassRef, "<init>", "()V"); 
jobject javaObjectRef = env->NewObject(javaClassRef, constructor); 

署名に自分のタイプを追加し、constructorNewObjectに渡す:あなたはこのようなコードで参照を取得することができます。

+0

返事をありがとう。問題は、このクラスのコンストラクタがAndroidのアクティビティであるためです。 – LBes

+0

別のコメントで "自分で新しいアクティビティインスタンスを作成しているわけではない"と言ったので、私は混乱しています。既存のアクティビティのメソッドを呼び出したいだけですか? – samgak

+0

申し訳ありませんがわかりませんが、そうです。 – LBes

関連する問題