[英]JNI DETECTED ERROR IN APPLICATION: use of invalid jobject when calling NewObject for innerclass
This is how my java file looks like:这是我的 java 文件的样子:
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:这是我的 cpp 方法的样子:
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.在我的 cpp 代码中,我有一个名为 MyVector 的结构,类似于 java 类 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.它给了我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.谢谢@Botje、@Petesh 和@Seelenvirtuose,您的回答帮助我理解在 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?我解决问题的方法是,我按照此链接中的答案在 JNI 中,如何根据 IBM 的性能建议缓存类、方法 ID 和字段 ID? shared by @Petesh and made a global reference for the ID's of my inner class MyVector and also for my outer class MyActivity.
由@Petesh 共享,并为我的内部类 MyVector 和外部类 MyActivity 的 ID 做了一个全局引用。
So, for creating an object of inner class, this is what i had to do.所以,为了创建内部类的对象,这就是我必须做的。
auto jObj = env->NewObject(InnerClassID, InnerClassConstructorID, OuterclassObject, Params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.