无法将Integer对象从C传递给Java

时间:2012-12-14 13:48:53

标签: java android android-ndk java-native-interface

我在JNI中面临一个微不足道的(我相信)问题,即将一个Integer对象从C传递给Java。

这是非常简单的代码

public class CallByNative {



         public CallByNative(int a )
         {
             System.out.println("Constructor caleld");
         }

          public Integer objectMethod(Integer a )
          {
              System.out.println("object method");

                return 1;
          }


}

现在,当我尝试通过以下代码调用 objectMethod 时,我的代码会崩溃

jobject Java_com_example_hellojni_HelloJni_callbackJava(JNIEnv * env,jobject this) {

/*Initialize Class*/
jclass cls = (*env)->FindClass(env, "com/example/hellojni/CallByNative");
jmethodID methodID = (*env)->GetMethodID(env, cls, "<init>", "(I)V");
jobject thisObj=(*env)->NewObject(env,cls, methodID, 5);



jmethodID meth1 = (*env)->GetMethodID(env,cls, "objectMethod", "(Ljava/lang/Integer;)Ljava/lang/Integer;");
number=10; // can't send this to java , since this is a primitive type I.E int . 

//Construct an int object
jclass clsInt = (*env)->FindClass(env, "java/lang/Integer");
jmethodID methodID1 = (*env)->GetMethodID(env, clsInt , "<init>", "(I)V");
jobject intObject= (*env)->NewObject(clsInt, methodID1, number);

jstring str=(jstring) (*env)->CallObjectMethod(env,cls,meth1,intObject);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK: Value returned from java: [%s]", str);

return thisObj;

}

当这种情况发生时,我会受到这种无法提供信息的堆栈跟踪的欢迎。

1

12-14 18:40:59.534: I/System.out(1088): Debugger has connected
12-14 18:40:59.534: I/System.out(1088): waiting for debugger to settle...
12-14 18:40:59.734: I/System.out(1088): waiting for debugger to settle...
12-14 18:40:59.934: I/System.out(1088): waiting for debugger to settle...
12-14 18:41:00.136: I/System.out(1088): waiting for debugger to settle...
12-14 18:41:00.334: I/System.out(1088): waiting for debugger to settle...
12-14 18:41:00.534: I/System.out(1088): waiting for debugger to settle...
12-14 18:41:00.734: I/System.out(1088): waiting for debugger to settle...
12-14 18:41:00.934: I/System.out(1088): waiting for debugger to settle...
12-14 18:41:01.194: I/System.out(1088): debugger has settled (1370)
12-14 18:41:01.814: D/dalvikvm(1088): Trying to load lib /data/data/com.example.hellojni/lib/libhello-jni.so 0x45ebe098
12-14 18:41:01.814: D/dalvikvm(1088): Added shared lib /data/data/com.example.hellojni/lib/libhello-jni.so 0x45ebe098
12-14 18:41:01.814: D/dalvikvm(1088): No JNI_OnLoad found in /data/data/com.example.hellojni/lib/libhello-jni.so 0x45ebe098, skipping init
12-14 18:41:01.854: I/System.out(1088): 31
12-14 18:41:01.886: I/System.out(1088): Hello. This is a test sentence.
12-14 18:41:01.904: I/System.out(1088): Hello. This is a test sentence.This is appended
12-14 18:41:01.914: I/System.out(1088): Constructor caleld
12-14 18:41:02.094: I/DEBUG(31): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
12-14 18:41:02.094: I/DEBUG(31): Build fingerprint: 'generic/sdk/generic/:2.2/FRF91/43546:eng/test-keys'
12-14 18:41:02.114: I/DEBUG(31): pid: 1088, tid: 1088  >>> com.example.hellojni <<<
12-14 18:41:02.114: I/DEBUG(31): signal 11 (SIGSEGV), fault addr 0000000a
12-14 18:41:02.114: I/DEBUG(31):  r0 0000000a  r1 00000007  r2 80886910  r3 00000088
12-14 18:41:02.114: I/DEBUG(31):  r4 0000aa50  r5 45ec5dc0  r6 00000007  r7 45ec5e68
12-14 18:41:02.114: I/DEBUG(31):  r8 42297820  r9 4208bcb4  10 0000ce04  fp 4208bcb0
12-14 18:41:02.114: I/DEBUG(31):  ip 808882a4  sp bebab848  lr 809010ef  pc 80901024  cpsr 00000030
12-14 18:41:02.204: I/DEBUG(31):          #00  pc 00001024  /data/data/com.example.hellojni/lib/libhello-jni.so
12-14 18:41:02.204: I/DEBUG(31):          #01  lr 809010ef  /data/data/com.example.hellojni/lib/libhello-jni.so
12-14 18:41:02.204: I/DEBUG(31): code around pc:
12-14 18:41:02.204: I/DEBUG(31): 80901004 1c026823 6f1e1c29 23051c20 bd7047b0 
12-14 18:41:02.204: I/DEBUG(31): 80901014 00001842 00001848 0000184c 1c0eb5f8 
12-14 18:41:02.204: I/DEBUG(31): 80901024 490c6803 699b1c04 47984479 1c054a0a 
12-14 18:41:02.214: I/DEBUG(31): 80901034 68204b0a 447a2184 447b5847 1c201c29 
12-14 18:41:02.214: I/DEBUG(31): 80901044 682347b8 1c291c02 1c206f1f 47b81c33 
12-14 18:41:02.214: I/DEBUG(31): code around lr:
12-14 18:41:02.214: I/DEBUG(31): 809010cc 682147e0 4b164a15 447a598e 447b1c29 
12-14 18:41:02.214: I/DEBUG(31): 809010dc 47b01c20 23886822 200a4680 f7ff58d6 
12-14 18:41:02.214: I/DEBUG(31): 809010ec 1c29ff99 46421c03 47b01c20 4a0e490d 
12-14 18:41:02.214: I/DEBUG(31): 809010fc 44791c03 2003447a eec0f7ff bc041c38 
12-14 18:41:02.214: I/DEBUG(31): 8090110c bdf04690 46c0e7fb 000017de 000017be 
12-14 18:41:02.214: I/DEBUG(31): stack:
12-14 18:41:02.214: I/DEBUG(31):     bebab808  42297768  /dev/ashmem/dalvik-LinearAlloc (deleted)
12-14 18:41:02.214: I/DEBUG(31):     bebab80c  80902890  /data/data/com.example.hellojni/lib/libhello-jni.so
12-14 18:41:02.214: I/DEBUG(31):     bebab810  0000ccb0  [heap]
12-14 18:41:02.214: I/DEBUG(31):     bebab814  0000ccb0  [heap]
12-14 18:41:02.214: I/DEBUG(31):     bebab818  00000063  
12-14 18:41:02.214: I/DEBUG(31):     bebab81c  80841a6d  /system/lib/libdvm.so
12-14 18:41:02.214: I/DEBUG(31):     bebab820  0000aa50  [heap]
12-14 18:41:02.214: I/DEBUG(31):     bebab824  45ec5dc0  /dev/ashmem/mspace/dalvik-heap/2 (deleted)
12-14 18:41:02.224: I/DEBUG(31):     bebab828  80902880  /data/data/com.example.hellojni/lib/libhello-jni.so
12-14 18:41:02.224: I/DEBUG(31):     bebab82c  80831d81  /system/lib/libdvm.so
12-14 18:41:02.224: I/DEBUG(31):     bebab830  00000000  
12-14 18:41:02.224: I/DEBUG(31):     bebab834  8086dd20  /system/lib/libdvm.so
12-14 18:41:02.224: I/DEBUG(31):     bebab838  bebab85c  [stack]
12-14 18:41:02.224: I/DEBUG(31):     bebab83c  bebab85c  [stack]
12-14 18:41:02.224: I/DEBUG(31):     bebab840  df002777  
12-14 18:41:02.224: I/DEBUG(31):     bebab844  e3a070ad  
12-14 18:41:02.224: I/DEBUG(31): #00 bebab848  00000088  
12-14 18:41:02.224: I/DEBUG(31):     bebab84c  0000aa50  [heap]
12-14 18:41:02.224: I/DEBUG(31):     bebab850  45ec5dc0  /dev/ashmem/mspace/dalvik-heap/2 (deleted)
12-14 18:41:02.224: I/DEBUG(31):     bebab854  80836f71  /system/lib/libdvm.so
12-14 18:41:02.224: I/DEBUG(31):     bebab858  45ec5e68  /dev/ashmem/mspace/dalvik-heap/2 (deleted)
12-14 18:41:02.224: I/DEBUG(31):     bebab85c  809010ef  /data/data/com.example.hellojni/lib/libhello-jni.so
12-14 18:41:02.234: I/DEBUG(31):     bebab860  bebab878  [stack]
12-14 18:41:02.234: I/DEBUG(31):     bebab864  bebab898  [stack]
12-14 18:41:02.234: I/DEBUG(31):     bebab868  00000004  
12-14 18:41:02.234: I/DEBUG(31):     bebab86c  4816eb43  /data/dalvik-cache/data@app@com.example.hellojni-1.apk@classes.dex
12-14 18:41:02.234: I/DEBUG(31):     bebab870  4208bcbc  
12-14 18:41:02.234: I/DEBUG(31):     bebab874  80813978  /system/lib/libdvm.so
12-14 18:41:02.234: I/DEBUG(31):     bebab878  42296af4  /dev/ashmem/dalvik-LinearAlloc (deleted)
12-14 18:41:02.234: I/DEBUG(31):     bebab87c  0000ccb0  [heap]
12-14 18:41:02.234: I/DEBUG(31):     bebab880  4816eb43  /data/dalvik-cache/data@app@com.example.hellojni-1.apk@classes.dex
12-14 18:41:02.234: I/DEBUG(31):     bebab884  bebab980  [stack]
12-14 18:41:02.234: I/DEBUG(31):     bebab888  42296bf8  /dev/ashmem/dalvik-LinearAlloc (deleted)
12-14 18:41:02.234: I/DEBUG(31):     bebab88c  00000374  
12-14 18:41:02.705: I/BootReceiver(59): Copying /data/tombstones/tombstone_07 to DropBox (SYSTEM_TOMBSTONE)
12-14 18:41:02.724: D/Zygote(33): Process 1088 terminated by signal (11)
12-14 18:41:02.724: I/ActivityManager(59): Process com.example.hellojni (pid 1088) has died.

请帮帮我

2 个答案:

答案 0 :(得分:0)

您可以使用SIGSEGV中的ndk-stack检查转储

答案 1 :(得分:0)

以下行错误:

jstring str=(jstring) (*env)->CallObjectMethod(env,cls,meth1,intObject);

第二个参数错误,并且由于该方法返回 Integer 实例,因此您不应将其分配给字符串变量。

正确的电话可能是:

jobject result = (*env)->CallObjectMethod(env, thisObj, meth1, intObject);