SetObjectField doesn't work with enum fields?

229 views
Skip to first unread message

Michal Seliga

unread,
Apr 26, 2011, 8:56:44 AM4/26/11
to android-ndk
I added to every of my enums static method which returns correct enum
value for int parameter
i can call it from java without any problem: MyEnum
data=MyEnum.fromInt(1);

this works well from android-ndk too, but i get exception when i try
to set received value to field
Exception is I/dalvikvm( 612): Ljava/lang/
CloneNotSupportedException;: Class doesn't implement Cloneable
i read that implementing clone for enums is generally not good idea,
so i didn't even tried it that way

here is code i am using:

jclass spdclass=env->FindClass("path/to/MyEnum");
if(spdclass!=NULL)
{
jmethodID cid = env->GetStaticMethodID(spdclass,"fromInt",
"(I)Lpath/to/MyEnum;");
if(cid != NULL)
{
jobject spd=env->CallObjectMethod(spdclass, cid, 1);
if(spd!=NULL)
{
//i succesfuly get here
env->SetObjectField(obj,fid,spd); // here i get
exception
}
}
}


is there any other way how to set content of enum field?

fadden

unread,
Apr 26, 2011, 7:02:42 PM4/26/11
to android-ndk
On Apr 26, 5:56 am, Michal Seliga <selmi....@gmail.com> wrote:
> Exception is I/dalvikvm(  612): Ljava/lang/
> CloneNotSupportedException;: Class doesn't implement Cloneable

This usually means that you tried to call a virtual method when you
wanted a static method. clone() appears early in the vtable.

>      jmethodID cid = env->GetStaticMethodID(spdclass,"fromInt",
> "(I)Lpath/to/MyEnum;");
>      if(cid != NULL)
>      {
>            jobject spd=env->CallObjectMethod(spdclass, cid, 1);

A jmethodID retrieved with GetStaticMethodID should only be used with
CallStaticObjectMethod. As of 2.3 ("gingerbread"), the CheckJNI
feature will detect this mistake automatically and complain.

Note that the return value from the CallMethod functions is undefined
when an exception is pending, so your test for NULL is insufficient.
You should always check for an exception (with e.g. ExceptionCheck)
before proceeding.
Reply all
Reply to author
Forward
0 new messages