2016-01-30 107 views
2

我想在c中創建一個新的數組,並保持原始數組加倍。這是我的C代碼,它編譯,但應用程序崩潰:jni - 將int [] []轉換爲jobjectArray並將其返回給java

#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的一面呢?拜託我需要你的幫忙!

+0

'a'從哪裏來?您正在返回一個自動變量('array'),該函數在函數返回後不再有效。 – Ctx

+0

@Ctx它是我想返回的數組。 – yanisB

+2

看看這裏:http://stackoverflow.com/questions/1610045/how-to-return-an-array-from-jni-to-java這應該會幫助你如何從jni返回一個合適的java int數組 – Ctx

回答

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; 
} 
+0

嘗試過,但我在logcat中得到這個錯誤:'由於:java.lang.ArrayStoreException:int []不能存儲在一個java.lang.Integer類型的數組中'[ ]任何幫助? – yanisB

+0

我更新了這個'(* env) - > FindClass(「[[I」);';現在試試 – milevyo

+0

非常感謝@milevyo,它的工作。 PS:我編輯了你的答案給我的工作版本(我加了#include 來識別NULL ....)。謝謝 – yanisB