android jni invokeMethod错误

时间:2018-06-05 03:19:08

标签: android java-native-interface

以下是我的代码

JNIEXPORT jstring JNICALL Java_demo_emi365_com_natives_Hello_sayHello
        (JNIEnv *env, jclass clazz){
    char* classname = "demo/emi365/com/natives/Hello";
    jmethodID methodID = (*env)->GetMethodID(env, clazz,"function", "(Ljava/lang/String;)I");
    jstring param = (*env)->NewStringUTF(env, "C中调用Java的String参数方法");
    (*env)->CallIntMethod(env,clazz,methodID,NULL);
    return (*env)->NewStringUTF(env,"hello");
}

如果注释CallIntMethod一切正常,但添加将崩溃,以下是错误信息:

    pid: 6893, tid: 6893, name: demo.emi365.com  >>> demo.emi365.com <<<
06-05 11:23:32.398 252-252/? I/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
06-05 11:23:32.554 252-252/? I/DEBUG:     eax 00000000  ebx 00001aed  ecx 00001aed  edx 00000006
06-05 11:23:32.554 252-252/? I/DEBUG:     esi 00001aed  edi 0000000b
06-05 11:23:32.554 252-252/? I/DEBUG:     xcs 00000073  xds 0000007b  xes 0000007b  xfs 00000000  xss 0000007b
06-05 11:23:32.554 252-252/? I/DEBUG:     eip 4006d0a6  ebp 400d1ce0  esp bfcb5490  flags 00200203
06-05 11:23:32.554 252-252/? I/DEBUG: backtrace:
06-05 11:23:32.554 252-252/? I/DEBUG:     #00  pc 0003c0a6  /system/lib/libc.so (tgkill+22)
06-05 11:23:32.554 252-252/? I/DEBUG:     #01  pc 00000005  <unknown>
06-05 11:23:32.554 252-252/? I/DEBUG: stack:
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5450  00000000  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5454  400cd268  /system/lib/libc.so
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5458  00000000  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb545c  4003efe9  /system/lib/libc.so (pthread_mutex_unlock+25)
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5460  400cd18c  /system/lib/libc.so
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5464  80255600  [heap]
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5468  00000015  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb546c  4003efe9  /system/lib/libc.so (pthread_mutex_unlock+25)
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5470  00000000  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5474  4008bad6  /system/lib/libc.so (funlockfile+6)
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5478  400ccfcc  /system/lib/libc.so
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb547c  400770f6  /system/lib/libc.so (__sflush_locked+150)
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5480  00000000  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5484  00000000  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5488  4003ee19  /system/lib/libc.so (pthread_mutex_lock+9)
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb548c  400ccfcc  /system/lib/libc.so
06-05 11:23:32.554 252-252/? I/DEBUG:     #00  bfcb5490  00000006  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5494  00001aed  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb5498  400ccfcc  /system/lib/libc.so
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb549c  40049826  /system/lib/libc.so (pthread_kill+102)
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54a0  00001aed  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54a4  00001aed  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54a8  00000006  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54ac  00000000  
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54b0  400cd268  /system/lib/libc.so
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54b4  4273ece1  /system/lib/libdvm.so
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54b8  400497c9  /system/lib/libc.so (pthread_kill+9)
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54bc  400ccfcc  /system/lib/libc.so
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54c0  bfcb550c  [stack]
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54c4  bfcb574c  [stack]
06-05 11:23:32.554 252-252/? I/DEBUG:          bfcb54c8  42ea29f0  

/ dev / ashmem / dalvik-heap(已删除)     06-05 11:23:32.554 252-252 /?我/ DEBUG:bfcb54cc 40049bec /system/lib/libc.so(加注+44)

1 个答案:

答案 0 :(得分:0)

如果原生方法function()不是静态,则需要 jobject ,而不是 jclasss 作为第二个参数。< / p>

如果静态,如下所示,则必须通过(*env)->CallStaticIntMethod( ... )进行调用。

package demo.emi365.com.natives;

public class Hello { 
    public String native static sayHello();
    public int static function(String param) {
        return 1;
    }
}