我正在用java做ndk但是在logcat中遇到了这些错误。
11-02 12:36:43.582: E/Trace(717): error opening trace file: No such file or
directory (2)
11-02 12:36:43.902: D/dalvikvm(717): Trying to load lib
/data/data/com.example.hellojni/lib/libhello-jni.so 0x411e35b8
11-02 12:36:43.918: D/dalvikvm(717): Added shared lib
/data/data/com.example.hellojni/lib/libhello-jni.so 0x411e35b8
11-02 12:36:43.918: D/dalvikvm(717): No JNI_OnLoad found in
/data/data/com.example.hellojni/lib/libhello-jni.so 0x411e35b8, skipping init
11-02 12:36:43.953: W/dalvikvm(717): Invalid indirect reference 0x2a00b9e0 in
decodeIndirectRef
11-02 12:36:43.953: E/dalvikvm(717): VM aborting
11-02 12:36:43.953: A/libc(717): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1),
thread 717 (xample.hellojni)
我的java和本机代码是这样的。我想在原生空间中添加两个数组元素。
JAVA CODE
package com.example.hellojni;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class HelloJni extends Activity{
public native double[] additionfromJNI(double[] array1,double[] array2);
double matrix1[] = new double[4];
double matrix2[] = new double[4];
double matrix3[] = new double[4];
String matrix4 = "";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
for(int i=0;i<4;i++){
matrix1[i]=1.0;
matrix2[i]=2.0;
}
matrix3= additionfromJNI(matrix1,matrix2);
for(int i=0;i<4;i++){
matrix4= matrix4+ Double.toString(matrix3[i])+",";
}
tv.setText(matrix4);
setContentView(tv);
}
static {
System.loadLibrary("hello-jni");
}
}
本地代码
#include <string.h>
#include <jni.h>
jdoubleArray
Java_com_example_hellojni_HelloJni_additionfromJNI
( JNIEnv* env,jobject thiz, jdoubleArray mat1 ,jdoubleArray mat2 )
{
jboolean isCopy1;
jboolean isCopy2;
jsize length= 16;
jint i=0;
jdouble *result;
jdouble* srcArrayElems1 = (*env)->GetDoubleArrayElements(env,mat1, &isCopy1);
jint n = (*env) -> GetArrayLength(env,mat1);
jdouble* srcArrayElems2 = (*env)->GetDoubleArrayElements(env,mat2, &isCopy2);
for(i=0; i<n; i++){
result[i]=srcArrayElems1[i]+srcArrayElems2[i];
}
if (isCopy1 == JNI_TRUE) {
(*env) -> ReleaseDoubleArrayElements(env,mat1, srcArrayElems1,
JNI_ABORT);
}
if (isCopy2 == JNI_TRUE) {
(*env) -> ReleaseDoubleArrayElements(env,mat2, srcArrayElems2,
JNI_ABORT);
}
return result;
}
任何人都可以帮我解决这些问题...
答案 0 :(得分:1)
你无法返回jdouble。您必须使用NewDoubleArray创建一个jdoubleArray对象,然后设置jdouble中的值。
BTW标题并不能反映您的实际问题。未调用JNI_OnLoad,因为您尚未定义JNI_OnLoad,但这不是错误。它被记录为D,这是一个调试消息。