我正在尝试按照 cpp 文件中的代码从 java 文件中接收 arraylist。这是代码
jobjectArray trackables;
JNIEXPORT void JNICALL
Java_com_putitout_buck_VideoPlayback_setTrackables(JNIEnv *env, jobject obj, jobject trackables_)
{
// use the Array list
jclass ArrayList_class = env->FindClass( "java/util/ArrayList" );
// to conver jobject to jstring
jmethodID caster = env->GetMethodID(ArrayList_class, "toString", "()Ljava/lang/String;");
// get two methods
jmethodID Get_method = env->GetMethodID( ArrayList_class, "get", "(I)Ljava/lang/Object" );
jmethodID Size_method = env->GetMethodID( ArrayList_class, "size", "()I" );
// call java.lang.ArrayList.get()
int NumElts = env->CallIntMethod(trackables_, Size_method);
// allocate output array
jclass objClass = env->FindClass("java/lang/String");
trackables = env->NewObjectArray(NumElts, objClass, 0);
// fetch all the items
for(int i = 0 ; i < NumElts ; i++)
{
// call java.lang.ArrayList.get(int index) method
// Not sure about the parameter passing here
jobject Tmp = env->CallObjectMethod(trackables_, Get_method, i);
jstring Str = (jstring)env->CallObjectMethod(Tmp, caster);
__android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "%s", Str);
// get the length
int StrLen = env->GetStringLength(Str);
env->SetObjectArrayElement(trackables, i, Str);
}
}
以下是我在 java 文件中声明的代码
public native void setTrackables(ArrayList<String> trackables);
以下是在日志中。
10-24 17:45:44.555: W/dalvikvm(27400): JNI WARNING: JNI method called with exception pending
10-24 17:45:44.555: W/dalvikvm(27400): in Lcom/putitout/buck/VideoPlayback;.setTrackables:(Ljava/util/ArrayList;)V (GetMethodID)
10-24 17:45:44.555: W/dalvikvm(27400): Pending exception is:
10-24 17:45:44.555: E/dalvikvm(27400): VM aborting
10-24 17:45:44.560: A/libc(27400): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)