JNI在应用程序中检测到错误:使用挂起异常java.lang.ClassNotFoundException调用JNI GetMethodID:未找到类

时间:2016-08-09 10:10:45

标签: android android-ndk-r5

我收到以下错误:

  

JNI在应用程序中检测到错误:使用挂起异常java.lang.ClassNotFoundException调用JNI GetMethodID:在路径上找不到类“package.name.class”:DexPathList [[zip file“/system/framework/sample.jar “],nativeLibraryDirectories = [/ vendor / lib,/ system / lib]]

JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception java.lang.ClassNotFoundException: Didn't find class "package.name.class" on path: DexPathList[[zip file "/system/framework/XposedBridge.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
26771-26918/package.name.class A/art: art/runtime/java_vm_ext.cc:410]   at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
26771-26918/package.name.class A/art: art/runtime/java_vm_ext.cc:410]   at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:511)
26771-26918/package.name.class A/art: art/runtime/java_vm_ext.cc:410]   at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:469)

以下是关于错误的更多信息:

`jmethodID MethodId;
    JNIEnv * env;
    int nStatus = gVM->GetEnv((void**)&env, JNI_VERSION_1_6);
    int nResult = attachStatus(nStatus, &env);
    LOG_HM("::attach status = %d::\n", nResult);
    if (nResult == ATTACH_FAILED) {
        LOG_HM("Attach failed");
        return ;
    }

jclass classClass = env->GetObjectClass(gTotalUsageClass);
    auto classLoaderClass = env->FindClass("java/lang/ClassLoader");
    auto getClassLoaderMethod = env->GetMethodID(classClass, "getClassLoader",
                                             "()Ljava/lang/ClassLoader;");
    gClassLoader = env->CallObjectMethod(gTotalUsageClass, getClassLoaderMethod);
    gFindClassMethod = env->GetMethodID(classLoaderClass, "findClass",
                                    "(Ljava/lang/String;)Ljava/lang/Class;");

    jclass totalUsage = static_cast<jclass>(env->CallObjectMethod(gClassLoader, gFindClassMethod, env->NewStringUTF("com/a/a/TotalUsageInfo")));

    TotalUsageInfo *info = NULL;
    jobject jUsageInfo = NULL;

        jlong noOfProc = 0;
        jlong memTotal = 0;
        jlong memFree = 0;
        jlong cache = 0;
        jlong buffer = 0;
        jlong cpuUtil = 0;
        //jclass totalUsage = env->FindClass("com/a/a/TotalUsageInfo");
        if (totalUsage == NULL) {
            LOG_HM("Class TotalUsageInfo not Found \n");
            nResult = FAIL;
            detachThread(nResult);
            return;
        }
        else {
             LOG_HM("Class TotalUsageInfo Found \n");
        }

        jmethodID constructor = env->GetMethodID(totalUsage, "<init>", "(JJJJJJ)V");
        if (NULL == constructor) {
            LOG_HM("JNIGetTotalUsageInfo::TotalUsageInfo constructor not found\n");
            nResult = FAIL;
            detachThread(nResult);
            return ;
        }`

1 个答案:

答案 0 :(得分:1)

要调用GetMethodID,您需要将C ++线程正确附加到主Java线程。像这样:

bool attachThreadToJVM(JNIEnv **env)
{

    bool ret = false;

    if(globalJavaVm != NULL) {
        JavaVMAttachArgs vmAttachArgs;
        vmAttachArgs.version = JNI_VERSION_1_6;
        vmAttachArgs.name = NULL;
        vmAttachArgs.group = NULL;
        jint attachRet = globalJavaVm->AttachCurrentThread(env, &vmAttachArgs);

        if(attachRet == 0)
        {
            ret = true;
        }
    }

    return ret;
}