すべてのネイティブ関数をラップするために次のコードを使用しています
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)
誰かが私を助けることができますか?