c++ 为什么“java.lang.NoSuchMethodError:no static method”from(JNIenv*)->GetStaticMethodID?

ss2ws0br  于 2023-08-09  发布在  Java
关注(0)|答案(2)|浏览(406)

我的Android NDK项目的Java Code调用C++函数,

extern "C" JNIEXPORT jstring JNICALL Java_com_crimson_tub_MainActivity_stringFromJNI(JNIEnv *env, jobject jo)

字符串
我想打电话

public static void requestPermissions(@NonNull final Activity activity, @NonNull final String[] permissions, @IntRange(from = 0L) final int requestCode)


在Android文档中找到:
https://developer.android.com/reference/android/support/v4/app/ActivityCompat.html#requestPermissions(android.app.Activity,java.lang.String[],int)
我已经尝试了一些代码的变化,我认为问题在于

jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Landroid/app/Activity;[Ljava/lang/String;I)V;"),
//jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Lcom/company/project/MainActivity;[Ljava/lang/String;I)V;"),
  //jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Ljava/lang/Object;[Ljava/lang/String;I)V;"),


所有上述替代方案导致相同的例外,

No pending exception expected: java.lang.NoSuchMethodError: no static method "Landroid/support/v4/app/ActivityCompat;.requestPermissions(Landroid/app/Activity;[Ljava/lang/String;I)V;"


C++代码

extern "C" JNIEXPORT jstring JNICALL
    Java_com_crimson_tub_MainActivity_stringFromJNI(JNIEnv *env,jobject jo) {

        std::string hello;

        jobjectArray ret;
        int i;

        char *data[2]= { "android.permission.CAMERA" ,
                 "android.permission-group.CALENDAR"};

        ret= (jobjectArray)env->NewObjectArray(2,env->FindClass("java/lang/String"),env->NewStringUTF(""));

        for(i=0;i<2;i++) env->SetObjectArrayElement(ret,i,env->NewStringUTF(data[i]));

        jint result = 0xffffffff;

        jclass clzz = env->FindClass("android/support/v4/app/ActivityCompat");
        if (!clzz){
            hello += "-class";
        }else{
            hello += "+class";
        }

        jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Landroid/app/Activity;[Ljava/lang/String;I)V;"),
        //jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Lcom/company/project/MainActivity;[Ljava/lang/String;I)V;"),
          //jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Ljava/lang/Object;[Ljava/lang/String;I)V;"),

        env->CallStaticVoidMethod(clzz,jmid,jo,ret,result);

        env->DeleteGlobalRef(jo);
        return env->NewStringUTF(hello.c_str());

    }


我希望能够通过JNI调用Android Java函数,例如,

ActivityCompat.requestPermissions


直接从C++中删除,这样以后我就可以从我的项目中删除Java代码了。
我已经在这个问题上工作了很长一段时间,仍然没有成功。
感谢所有的帮助,这是非常赞赏。
谢谢大家。

cs7cruho

cs7cruho1#

问题是;在签名的V后面。现在起作用了。哈哈

mv1qrgav

mv1qrgav2#

在签名中有I、J和V时,我们不使用;在这些之后。

相关问题