为什么从(JNIenv *)-> GetStaticMethodID中出现“ java.lang.NoSuchMethodError:无静态方法”?

时间:2019-04-10 09:24:13

标签: c++ android-ndk jnienv

我的Android NDK项目的Java代码调用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代码。

我已经在这个问题上工作了一段时间,但仍然没有成功。

感谢所有帮助,我们非常感谢。

谢谢大家。

1 个答案:

答案 0 :(得分:0)

问题是;在签名中的V之后。现在可以了。大声笑。