JNI - 转换INT [] []将jobjectArray并返回到Java [英] jni - convert int[][] to jobjectArray and return it to java
本文介绍了JNI - 转换INT [] []将jobjectArray并返回到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建下保持原始数组一倍的新数组。这是我的C code,它编译,但应用程序崩溃:
I want to create a new array in c holding original array doubled. this is my c code, It compiles but the app crashes:
#include <jni.h>
JNIEXPORT jobjectArray JNICALL Java_com_example_jninew_MainActivity_getNum(JNIEnv *env, jobject obj, jintArray arr)
{
int i,j, sum = 0;
jsize width = (*env)->GetArrayLength(env, arr);
int array[2][2];
for (i=0; i<width; i++){
jintArray *line = (*env)->GetObjectArrayElement(env, arr, i);
int height = (*env)->GetArrayLength(env, line);
jint *pos = (*env)->GetIntArrayElements(env, line, 0);
for (j=0; j<height; j++){
array[i][j] = 2*pos[j];
}
(*env)->ReleaseIntArrayElements(env, arr, pos, 0);
}
return array;
}
主要的Java code:
Main java code:
package com.example.jninew;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.tv);
int[][] a = {{1,2},{3,55}};
a = getNum(a);
textView.setText("G"+a[0][1]);
}
static{
System.loadLibrary("getNum");
}
native int[][] getNum(int[][] a);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
你能帮助我返回数组到Java的一面呢?请我需要你的帮助!
Can you help me to return the array to java side? Please I need your help!
推荐答案
根据我阅读手册,这里是我到目前为止降落。
According to manuals that i read, here is where i land so far.
#include <jni.h>
#include<stddef.h>
JNIEXPORT jobjectArray JNICALL Java_com_example_jninew_MainActivity_getNum(JNIEnv *env, jobject obj, jobjectArray arr)
{
int i,j, sum = 0;
jclass intClass = (*env)->FindClass(env,"[I");//
jsize width = (*env)->GetArrayLength(env, arr);
jobjectArray jObjarray = (*env)->NewObjectArray(env,width,intClass, NULL); //**//
for (i=0; i<width; i++){
jintArray *line = (*env)->GetObjectArrayElement(env, arr, i);
int height = (*env)->GetArrayLength(env, line);
jintArray jline = (*env)->NewIntArray(env,height); //**//
jint *pos = (*env)->GetIntArrayElements(env, line, 0);
jint jpos[ height ];
for (j=0; j<height; j++){
jpos[j] = 2*pos[j];
}
(*env)->SetIntArrayRegion(env, jline, 0, height, jpos); //**//
(*env)->ReleaseIntArrayElements(env, line, pos, 0);
(*env)->ReleaseIntArrayElements(env, jline, jpos, 0); //**//
(*env)->SetObjectArrayElement(env,jObjarray,i,jline);
(*env)->DeleteLocalRef (env,jline); //**//
}
return jObjarray;
}
这篇关于JNI - 转换INT [] []将jobjectArray并返回到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文