简体   繁体   中英

Null Pointer Exception in alert Dialog Builder call from JNI

I know that many questions have been asked about it and I have seen this post already that deals with this kind of errors but I'm in a special contest of JNI calls. Android JNI - Call AttachCurrentThread without DetachCurrentThread

So here is what I do: I have a separate c++ thread running some code and at some point (after a timer has ended) I need to call a callback function in java. This callback function is used to display some alerts and then run some code.

So basically here's my code c++ side:

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

And then the function that is called on the Java Side does the following: AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("Next Technique"); alert.setMessage("You will now try an other technique");

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

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

However I get the following error:

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] (system@framework@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)

An educated guess from my side is that this has to do with the UIThread . I suppose that the call from my thread in c++ to the java function is not on the UIThread and that's why I get an error. But I'm not sure that it actually is the problem and I have no idea on how I could solve that. Would any of you know what I could do.

Edit : So I tried removing all the code that deals with the AlertDialog and the issue does not occur. So it seems it's either a problem with the UIThread or a problem with the AlertDialog... But I'm guessing it's more of a UIThread issue as creating a simple AlertDialog outside of this function works.

There is a problem with your NewObject call.

The second parameter passed to NewObject is supposed to be a reference to the constructor, but in your case it's the same method that you call with 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.

You haven't shown the code where you initialize javaMethodRef so it's not clear whether it's the constructor or another method. If it's the constructor, you only need to call it once and the CallVoidMethod call is redundant. If it's not the constructor, you should be calling the constructor instead. You can get a reference with code similar to this:

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

If your constructor takes arguments, add their types to the signature and pass them to NewObject after constructor .

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