gpt4 book ai didi

c - jni - 将 int[][] 转换为 jobjectArray 并将其返回给 java

转载 作者:太空宇宙 更新时间:2023-11-03 23:45:11 24 4
gpt4 key购买 nike

<分区>

我想在 c 中创建一个新数组,将原始数组加倍。这是我的 C 代码,它编译但应用程序崩溃:

 #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代码:

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端吗?我需要你的帮助!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com