android JNI报告无法找到方法

时间:2016-10-09 06:54:44

标签: android java-native-interface nosuchmethoderror jnienv

我使用以下代码包装所有本机函数

package com.user.game;

import android.content.res.AssetManager;

public class MyGLRenderer {

    static {
        System.loadLibrary("Engine");
    }

    public static native void passAssetManager(AssetManager assetManager);
    public static native void getAppDirectory(String dir);
    public static native void setExternalDirectory(String dir);
    public static native void pointerLeave(int id, float x, float y);

}

我在C ++中使用以下内容

JavaVM* javaVM = NULL;

jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    javaVM = vm;
    return JNI_VERSION_1_6;
}

bool call_Java_checkStoragePermission() {
    JNIEnv *env;
    bool shouldDetach = false;
    static jclass javaClassRef = NULL;
    static jmethodID javaMethodRef = NULL;
    static bool once = true;

    if (javaVM->GetEnv((void **)&env, JNI_VERSION_1_6) == JNI_EDETACHED) {
        shouldDetach = true;
        javaVM->AttachCurrentThread(&env, NULL);
    }

    if (once) {
        jclass dataClass = javaClassRef = env->FindClass("com/quetzalfir/polygondash/Game_Activity");
        if (checkExcp(env, "findclass")) {
            return false;
        }

        javaClassRef = (jclass) env->NewGlobalRef(dataClass);
        if (checkExcp(env, "newGlobalRef")) {
            return false;
        }

        //BEFORE EDIT: this was env->GetMethodID();
        javaMethodRef = env->GetStaticMethodID(javaClassRef, "checkStoragePermission", "()Z");
        if (checkExcp(env, "get methodID")) {
            return false;
        }

        once = false;
    }

    jboolean ans = env->CallStaticBooleanMethod(javaClassRef, javaMethodRef);

    if (shouldDetach) {
        javaVM->DetachCurrentThread();
    }

    return ans;
}

bool checkExcp(JNIEnv *env, const char *str) {
    if (env->ExceptionCheck()) {
        SFLOGI("JNI: error %s",str);
        jthrowable flag = env->ExceptionOccurred();
        env->ExceptionClear();
        env->Throw(flag);

        return true;
    }

    return false;
}

我从我的主要活动中调用包装类

package com.user.game;

public class Game_Activity extends Activity {
    private static String TAG = "Debugging";

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        MyGLRenderer.getAppDirectory(getExternalFilesDir(null).getAbsolutePath());
        MyGLRenderer.setExternalDirectory(Environment.getExternalStorageDirectory().getPath());

        MyGLRenderer.passAssetManager(getAssets());
    }

    public static boolean checkStoragePermission() { //before edit Boolean

        Log.w(TAG, "calling...........");

        return true;
    }

}

但是当我致电call_Java_checkStoragePermission()时,我收到以下错误

10-09 16:34:48.502 18590-18590/com.user.game D/dalvikvm: GetMethodID: not returning static method Lcom/user/game/Game_Activity;.checkStoragePermission ()Z
10-09 01:15:17.081 14171-14171/com.user.game I/Debugging: JNI: error get methodID
10-09 01:15:17.081 14171-14171/com.user.game I/Debugging: bool = 0
10-09 01:15:17.081 14171-14171/com.user.game E/InputEventReceiver: Exception dispatching input event.
10-09 01:15:17.081 14171-14171/com.user.game E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
10-09 01:15:17.091 14171-14171/com.user.game E/MessageQueue-JNI: java.lang.NoSuchMethodError: no method with name='checkStoragePermission' signature='()Z' in class Lcom/user/game/Game_Activity;
                                                                                  at com.user.game.MyGLRenderer.pointerLeave(Native Method)

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

使用GetStaticMethodId作为静态函数:

1471361094509,doorLowerPosition,-73.3348875
1471361094509,doorUpperPosition,-3.29595
1471361094512,sectionLowerCurrFiltered,-0.2
1471361094512,actuatorLowerFrontDuty,0.0
1471361094515,doorCtrlStatus,5.0
1471361094515,SMState,14.0
1471361094516,lateralAccel,25.55
1471361094516,longitudinalAccel,25.55 
1471361094519,ambientTemperature,23.5