0

What I am trying to do is to create a JPoint(int []) using NDK. So I have created the JPoint class:

JPoint.java

public class JPoint {
    int x;
    int y;

   public JPoint(int[] xy)
   {
     x = xy[0];
     y = xy[1];
   }

   public JPoint(int x, int y) 
   {
     this.x = x;
     this.y = y;
   }

    public int getX() 
    {
      return x;       
    }
    public int getY() 
    {
      return y;
    }
}

in jni I have created this method

jni/sqlitenative.cpp

JNIEXPORT jobject JNICALL
Java_com_example_jnisqliteexperiment_MainActivity_getPersons(
            JNIEnv *env,
            jobject callingObject)
{
    jmethodID constructor;
    int x;
    int y;
    x = 10;
    y = 20;
    int xy[2] = {4, 5};

    jclass cls = env->FindClass("com/example/jnisqliteexperiment/JPoint");

    //creating new Jpoint(int[])
    constructor = env->GetMethodID( cls, "<init>", "([I)V");
    jobject obj = env->NewObject(cls, constructor,  xy);

    //creating new Jpoint(int, int)
    // constructor = env->GetMethodID(cls, "<init>", "(II)V");
    // jobject obj = env->NewObject(cls, constructor,  x ,y);
    return obj;
}

inside the MainActivity

protected void onCreate(Bundle savedInstanceState)
{
  // ..

  JPoint point = getPersons();
  textView.setText(String.valueOf(point.getX()));

  // ..
}

public native JPoint getPersons();

It works perfectly when I put as an argument (II)V instead of ([I)V in GetMethodID. So I can easily create a Point(int, int). But when I am trying to create a JPoint(int[]). I take a VM aborting in my Log, the Application starts without showing anything inside and the strange is, that my device starts vibrating.

  1. What am I doing wrong here ?

  2. Why does my device start vibrating without a profound reason ?

1 Answer 1

3
int xy[2]={4,5};

...

constructor = env->GetMethodID( cls, "<init>", "([I)V");
jobject obj = env->NewObject(cls, constructor,  xy);

The JPoint constructor takes a Java array as parameter, you are providing it a C array. This is probably what causes the VM crash.

Something similar to this should work:

int xy[2]={4,5};

...

jintArray jxy = env->NewIntArray(2);
env->SetIntArrayRegion(jxy, 0, 2, xy);
constructor = env->GetMethodID(cls, "<init>", "([I)V");
jobject obj = env->NewObject(cls, constructor,  jxy);

As for the vibration... no idea!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.