2

我正在使用 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代码

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;

        }

谁能帮我解决这些问题...

4

1 回答 1

1

你不能返回 jdouble。您必须使用 NewDoubleArray 创建一个 jdoubleArray 对象,然后从您的 jdouble 设置值。

顺便说一句,标题并不能反映您的实际问题。JNI_OnLoad 没有被调用,因为您还没有定义一个,但这不是错误。它被记录为 D,这是一条调试消息。

于 2012-11-02T07:33:03.623 回答