2012-11-19 95 views
3

我有一個簡單的問題,但不能在網絡上找到一個解決方案。JNI的GetMethodID返回null

我想簡單地從本地使用JNI檢索Java的String。

jint JNI_OnLoad(JavaVM *vm, void *reserved) { 
void * my_lib_handle; 

jmethodID mid; 
jstring resultString; 
JNIEnv *env; 
int status; 

__android_log_write(ANDROID_LOG_DEBUG,"JNI", "> JNI_OnLoad"); 
status = (*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_4); 
if(status < 0){ 
    __android_log_write(ANDROID_LOG_DEBUG,"JNI", "failed to get JNI environment"); 
    status = (*vm)->AttachCurrentThread(vm, (void**)&env, NULL); 
    if(status < 0){ 
     __android_log_write(ANDROID_LOG_DEBUG,"JNI", "failed to attach to current thread"); 
     return; 
    } 
} 
jclass handlerClass = (*env)->FindClass(env, "com/mypackage/JniInterface"); 

if (handlerClass == NULL) { 
    __android_log_write(ANDROID_LOG_DEBUG,"JNI", "can not find the class JniInterface "); 
    return; 
} 
mid = (*env)->GetMethodID(env, handlerClass, "getCert", "()Ljava/lang/String"); 
if (mid == NULL) { 
    __android_log_write(ANDROID_LOG_DEBUG,"JNI", "can not find the method getCert"); 
    return; 
} 

FindClass的作品。但它未能在GetMethodId

這裏是我的簡單類的聲明。我比以前更簡單:)

package com.mypackage; 
public class JniInterface { 
private static String cert; 
private static String key; 
public JniInterface(){ 

} 
public static String getCert() { 
    return cert; 
} 
public static void setCert(String cert) { 
    JniInterface.cert = cert; 
} 
public static String getKey() { 
    return key; 
} 
public static void setKey(String key) { 
    JniInterface.key = key; 
} 
} 

我被卡住了。我認爲這是一個愚蠢的錯誤...

能否請你幫我調查了嗎?

+0

在JNI方法簽名的字符串不要猜測。 javap的-s會告訴你有100%的可靠性。 – EJP

回答

10

你的方法簽名不正確,缺少尾隨分號:

"()Ljava/lang/String;"  
--------------------^ 
+0

這是微妙而重要的。它幫助了很多有尾隨分號,如果它出現在方法簽名。 –