想與NDK一起工作,我沒有與android工作室的運氣(直到現在我沒有得到指示NDK路徑的觀點,因爲我在終端之外做了一切IDE以外的任何代碼並且沒有完成代碼) ,我切換到eclipse,這使得它更容易與jni和ndk開發工作。JNI sum 2 dimentional array
首先,我創建了一個項目,在c中對一個2d的整數數組進行求和,並將求和返回給java端。我無法讓它工作。你能幫我嗎?!!
我的C代碼是:
#include <jni.h>
JNIEXPORT jint JNICALL Java_com_example_jninew_MainActivity_getNum(JNIEnv *env, jobject obj, jintArray arr)
{
int i,j, sum = 0;
jsize width = (*env)->GetArrayLength(env, arr);
jintArray *line = (*env)->GetIntArrayElements(env, arr, 0);
for (i=0; i<width; i++){
jint *pos = (*env)->GetIntArrayElements(env, line, i);
for (j=0; j<height; j++){
sum += pos[i][j];
}
}
(*env)->ReleaseIntArrayElements(env, arr, line, 0);
return sum;
}
我的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,4}};
textView.setText("sum is: "+getNum(a));
}
static{
System.loadLibrary("getNum");
}
native int getNum(int[][] a);
.
.
.}
是'JNIEXPORT jint JNICALL Java_com_example_jninew_MainActivity_getNum( JNIEnv * env,jobject obj,jintArray arr)'真的來自'javah'的輸出嗎?因爲二維數組是數組對象的數組,所以我期望看到......,jobjectArray arr)。請參閱http://stackoverflow.com/a/6752105/4756299 –
以及如何使用javah生成該文件? – yanisB
您需要使用GetObjectArrayElement從int [] []中獲取每個'int []',然後GetIntArrayElements。 – fadden