本文介绍了JNI在应用程序中检测到错误:调用JNI GetMethodID,未决异常java.lang.ClassNotFoundException:未找到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下错误:

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)

有关此错误的更多信息:

Here is more about the error:

`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 ;
        }`

推荐答案

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

In order to call GetMethodID, you need to properly attach your C++ thread to the main Java thread. Like this:

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

这篇关于JNI在应用程序中检测到错误:调用JNI GetMethodID,未决异常java.lang.ClassNotFoundException:未找到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 17:47