java - 在 JNI 调用中调用 getFilesDir() 时出现 NullPointerException

标签 java android c java-native-interface

我试图通过 JNI 调用从 C 库调用 Java 的 getFilesDir(),但遇到了我不理解的 NullPointerException。这是异常(exception)情况:

W/System.err( 1576): java.lang.NullPointerException
W/System.err( 1576):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
W/System.err( 1576):    at MYService.getFilesDirPath(MYService.java:1150)
W/System.err( 1576):    at MYService.UtvGetPeristentPath(Native Method)

问题发生在这个函数中:

public String getFilesDirPath()
{
    try {
        return getFilesDir().getPath(); // <--- Exception happens here!
    }
    catch(Exception e)
    {
        e.printStackTrace();
        Log.e("DEBUG", "getFilesDirPath set exception", e);
        return null;
    }
}

JNI 调用是通过 JNI_OnLoad 设置的一些静态变量来设置的:

static JavaVM              *s_jvm = NULL;
static jclass cls; 
static jmethodID mid;
static jobject obj;                        

jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    s_jvm = vm;
    JNIEnv *env;
    if (s_jvm){
        (*s_jvm)->AttachCurrentThread (s_jvm, &env, NULL);
    }    
    jclass localRef_class;
    jmethodID localRef_mid;
    jmethodID constr; 

    localRef_class = (*env)->FindClass(env, "MYService");
    cls = (*env)->NewGlobalRef(env,localRef_class);

    constr = (*env)->GetMethodID(env, cls, "<init>", "()V"); 
    obj = (*env)->NewGlobalRef(env, (*env)->NewObject(env, cls, constr));

    return JNI_VERSION_1_6; 
}

这是 JNI 函数本身:

char *getFilesDirPath() {
    JNIEnv *jenv = NULL;
    __android_log_print(ANDROID_LOG_ERROR, "DEBUG", "JNI_Interface: getFilesDirPath");
    if(s_jvm == NULL)
    return NULL;

    int check = (*s_jvm)->GetEnv(s_jvm,(void **)&jenv,JNI_VERSION_1_6);

    if (check != JNI_OK) {
        (*s_jvm)->AttachCurrentThread(s_jvm, &jenv, NULL);
        (*s_jvm)->GetEnv(s_jvm,(void **)&jenv,JNI_VERSION_1_6);
    }
    if(jenv != NULL)
    {

        // get the method
        mid = (*jenv)->GetMethodID(jenv, cls, "getFilesDirPath", "()Ljava/lang/String;");
        if (mid == 0) {
            __android_log_print(ANDROID_LOG_ERROR, "DEBUG", "getFilesDirPath not found");
            if (check != JNI_OK)
                (*s_jvm)->DetachCurrentThread (s_jvm);
            return NULL;
        }
        else{
            jstring result = (jstring)(*jenv)->CallObjectMethod(jenv, obj, mid);
            char *filesDir = (char *) (*jenv)->GetStringUTFChars(jenv, result, 0);
            if (check != JNI_OK)
                (*s_jvm)->DetachCurrentThread (s_jvm);
            return filesDir;
        }

    }

    if (check != JNI_OK)
        (*s_jvm)->DetachCurrentThread (s_jvm);
    return NULL;
}

问题肯定与 getFilesDir() 有关。如果我避免调用该函数并传回调试字符串,则一切似乎都可以正常工作。我认为问题与未正确设置上下文有关,但我不确定可能出了什么问题。

最佳答案

您的 MYService 类可能扩展了 Android 服务或 Activity 。这些类通常不是通过调用构造函数来创建的。它们由系统使用 Intents 启动。您通常可以在其 onCreate 方法中首次使用其完整的“上下文”功能。因此,您应该从其他地方获取对 MYService 的引用 - 而不是通过 JNI 实例化它。

关于java - 在 JNI 调用中调用 getFilesDir() 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24620770/

相关文章:

java - Thread.currentThread() 性能

android - 单击以选择微调器异常

c - 是否可以在 c 中定义大小变化结构?

c - 在数组中读取迷宫文件的数组,立方体大小不均,多余字符

c - 是什么导致了 SIGSEGV?

java - 无法获取列表的子列表

java - 注解会影响 Java 默认序列化吗?

java - Microsoft JDBC 驱动程序对 Java 代码有奇怪的行为

android - Espresso、Dagger2 在 BaseActivity 上设置 ViemodelProvider.Factory

java - 将数组列表从一个 Activity 发送到另一个 Activity 到 ListView android中