0
我正在使用JNI
在Android
中調用實例方法。所有的代碼編譯,但爲了保護項目的內容,我只會顯示代碼的相關部分。以下代碼塊(在C++文件中使用的extern c
)嘗試訪問cls中的靜態方法[public String getMyString()
]。我知道cls和env不爲空。 mid未設置爲零,也不是null。當這個本地方法返回Java方面,我得到以下錯誤(追溯到Class.java):JNI NoSuchMethodException:參數類型爲空
jmethodID mid = env->GetMethodID(cls, "getMyString", "()Ljava/lang/String;");
if (mid == 0) {
__android_log_write(ANDROID_LOG_INFO, "JNI", "mid is 0");
}
else {
__android_log_write(ANDROID_LOG_INFO, "JNI", "mid is not 0");
}
jstr = (jstring) env->CallObjectMethod(obj, mid);
if (jstr == NULL) {
__android_log_write(ANDROID_LOG_INFO, "JNI", "jstr is NULL");
}
else {
__android_log_write(ANDROID_LOG_INFO, "JNI", "jstr is not NULL");
}
printf(" c.s = \"%s\"\n", env->GetStringUTFChars(jstr, NULL));
return jstr;
雖然Class.java:
02-22 10:38:36.010: E/AndroidRuntime(17894): FATAL EXCEPTION: GLThread 10
02-22 10:38:36.010: E/AndroidRuntime(17894): java.lang.NoSuchMethodException: parameter type is null
02-22 10:38:36.010: E/AndroidRuntime(17894): at java.lang.ClassMembers.getConstructorOrMethod(ClassMembers.java:228)
02-22 10:38:36.010: E/AndroidRuntime(17894): at java.lang.Class.getMethod(Class.java:904)
02-22 10:38:36.010: E/AndroidRuntime(17894): at com.example.android.MyClass.getMyString(Native Method)
02-22 10:38:36.010: ...
產生誤差的代碼如下似乎表明它是從空到Class<?>[]
(常見的反射)投出了問題,我有一種感覺它有更多的事情要做中旬發現錯誤的方法或jstr回來我不正確(logcat狀態jstr不爲空)。
最後,Java端相關的代碼如下:
本機電話:
public static native String getMyNativeString();
到本地方法的調用:
String myStr = callGetMyNativeString();
的方法正被訪問:
public String getMyString() {
return "Foobar";
}