【问题标题】:jni - convert int[][] to jobjectArray and return it to java [duplicate]jni - 将 int[][] 转换为 jobjectArray 并将其返回给 java [重复]
【发布时间】:2016-01-30 16:04:46
【问题描述】:

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

【问题讨论】:

  • a 来自哪里?您正在返回一个自动变量 (array),该变量在函数返回后不再有效。
  • @Ctx 这是我要返回的数组。
  • 看看这里:stackoverflow.com/questions/1610045/… 这应该可以帮助您了解如何从 jni 返回正确的 java int 数组
  • 您的电话返回jint。那不是数组。
  • @AndrewHenle 我想返回一个二维数组,我将其更改为 jobjectArray。

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


【解决方案1】:

根据我阅读的手册,这是我目前着陆的地方。

#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;
}

【讨论】:

  • 试过了,但是我在 logcat 中得到了这个错误:` 原因:java.lang.ArrayStoreException: int[] cannot be stored in an array of type java.lang.Integer[]` 有什么帮助吗?
  • 我更新了这个(*env)-&gt;FindClass("[[I");;立即尝试
  • 非常感谢@milevyo,它正在工作。 PS:我编辑了您对我的工作版本的答案(我添加了#include 以识别NULL ....)。谢谢
猜你喜欢
  • 2013-06-12
  • 2016-11-20
  • 2014-03-30
  • 2013-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-10
  • 2011-10-25
相关资源
最近更新 更多