Android,JNI在C ++中调用java构造函数

时间:2017-06-19 17:28:14

标签: java android c++ java-native-interface method-call

我有以下java类:

 public class SHPObject {
    public int nSHPType;
    int nShapeId;
    public int nParts;
    int[] panPartStart;
    int panPartType;
    int nVertices;
    double[] padfX;
    double[] padfY;

    public SHPObject(int nSHPType, int nParts, int[] panPartStart, double[] padfX, double[] padfY ){
        this.nSHPType = nSHPType;
        this.nParts = nParts;
        this.panPartStart = panPartStart;
        this.padfX = padfX;
        this.padfY = padfY;
    }

 }

我正在尝试用c ++创建这个类的对象。这是我的代码:

    jclass shpObjectClass = env->FindClass("com/example/kaczor/tmc_shpreader/Shape/Shapes/SHPObject");
    jmethodID shpObjectConstructor = env->GetMethodID(shpObjectClass, "<init>", "(II[I[D[D)V");
    jobject recognition_result;
    SHPObject* a = new SHPObject[nEntities];
    for(i = 0; i < nEntities; i++){
        a[i] = *SHPReadObject(handle,i);
        recognition_result = env->NewObject(
                shpObjectClass, shpObjectConstructor, a[i].nSHPType, a[i].nParts, *a[i].panPartStart, *a[i].padfX, *a[i].padfY);
    ...
    }

遗憾的是,在执行此代码时,我的应用程序关闭了。虽然我在java中将构造函数和c ++中的方法签名更改为:

public SHPObject(int nSHPType, int nParts, int[] panPartStart)

methodID shpObjectConstructor = env->GetMethodID(shpObjectClass, "<init>", "(II[I)V");

一切正常。因此传递double []变量存在一些问题。我无法弄清楚出了什么问题。这是执行此代码后的堆栈跟踪的一部分(不确定我应该给它的哪一部分,我没有注意到任何引用错误的内容):

06-19 17:23:08.876 728-728/com.example.kaczor.tmc_shpreader W/dalvikvm: 
Invalid indirect reference 0xbcad9adc in decodeIndirectRef
06-19 17:23:08.876 728-728/com.example.kaczor.tmc_shpreader E/dalvikvm: VM 
aborting
06-19 17:23:08.906 728-728/com.example.kaczor.tmc_shpreader A/libc: Fatal 
signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 728 (r.tmc_shpreader)
[...]

编辑: 可以在以下位置找到C ++ SHPObject定义:http://shapelib.maptools.org/shp_api.html

1 个答案:

答案 0 :(得分:1)

所以我将double []转换为jdoubleArray,不知道为什么我没有转换int []。虽然我发现Java中int []的所有值都是0,所以我还是要转换它。

    jclass shpObjectClass = env->FindClass("com/example/kaczor/tmc_shpreader/Shape/Shapes/SHPObject");
    jmethodID shpObjectConstructor = env->GetMethodID(shpObjectClass, "<init>", "(IIIIIII[I[D[D)V");
    SHPObject* a = new SHPObject[nEntities];
    jobject recognition_result;
    jobjectArray shapesToReturn = env->NewObjectArray(nEntities,shpObjectClass,env->NewObject(shpObjectClass,defaultShpObjectConstructor));

    for(i = 0; i < nEntities; i++){
        a[i] = *SHPReadObject(handle,i);

        jdoubleArray padfXARRAY = env->NewDoubleArray(a[i].nVertices);
        env->SetDoubleArrayRegion(padfXARRAY, 0, a[i].nVertices, (jdouble *)a[i].padfX );
        jdoubleArray padfYARRAY = env->NewDoubleArray(a[i].nVertices);
        env->SetDoubleArrayRegion(padfYARRAY, 0, a[i].nVertices, (jdouble *)a[i].padfY );
jdoubleArray panPartStart = env->NewDoubleArray(a[i].nParts);
        env->SetDoubleArrayRegion(panPartStart, 0, a[i].nParts, (jdouble *)a[i].panPartStart );

        int shapeId = a[i].nShapeId;
        int shpType = a[i].nSHPType;
        int nParts = a[i].nParts;
        int dfXMin = a[i].dfXMin;
        int dfXmax = a[i].dfXMax;
        int dfYMax = a[i].dfYMax;
        int dfYMin = a[i].dfYMin;

        recognition_result = env->NewObject(
                        shpObjectClass, shpObjectConstructor, shapeId,shpType,nParts,dfXMin,dfXmax,dfYMin,dfYMax,panPartStart, padfXARRAY, padfYARRAY);
        env->DeleteLocalRef(padfXARRAY);
        env->DeleteLocalRef(padfYARRAY);
        env->SetObjectArrayElement(shapesToReturn,i,recognition_result);
    }