简体   繁体   中英

JNI DETECTED ERROR IN APPLICATION: use of invalid jobject when calling NewObject for innerclass

This is how my java file looks like:

public class MyActivity
  {
            public class MyVector
            {
                public float X;
                public float Y;
                public float Z;

                public MyVector()
                {
                    this.X = 0.0f;
                    this.Y = 0.0f;
                    this.Z = 0.0f;
                }

                public MyVector(float InX,float InY, float InZ)
                {
                    this.X = InX;
                    this.Y = InY;
                    this.Z = InZ;
                }

                public void SetMyVector(float InX,float InY, float InZ)
                {
                    X = InX;
                    Y = InY;
                    Z = InZ;
                }               
            }
  }

This is how my cpp method looks like:

static auto MyVectorClassID = env->FindClass("com/example/Test/MyActivity$MyVector");

static auto MyVectorParamCtorID = env->GetMethodID(MyVectorClassID, "<init>", "(Lcom/example/Test/MyActivity;FFF)V");

MyVector SomeVector{ 10.0f, 10.0f, 10.0f };

jfloat FloatX = SomeVector.X;
jfloat FloatY = SomeVector.Y;
jfloat FloatZ = SomeVector.Z;

auto jObj = env->NewObject(MyVectorClassID, MyVectorParamCtorID, FloatX, FloatY, FloatZ); // Crashes my android device here.

In my cpp code i have a struct named as MyVector similar to java class MyVector. I just don't know what i am doing wrong here.

It is giving me JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0xd18a1c78 when env->NewObject is called.

Thank you.

Thank you, @Botje, @Petesh and @Seelenvirtuose, your answers helped me understand working with inner classes in JNI. I am new to this.

The way i fixed my problem was, i followed the answer in this link In JNI, how do I cache the class, methodID, and fieldIDs per IBM's performance recommendations? shared by @Petesh and made a global reference for the ID's of my inner class MyVector and also for my outer class MyActivity.

So, for creating an object of inner class, this is what i had to do.

auto jObj = env->NewObject(InnerClassID, InnerClassConstructorID, OuterclassObject, Params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM