I am new to C# so I was doing some tutorials. I get this error when I try to run a tutorial Android project from Xamarin Studio:
A System.InvalidCastException was thrown. Cannot cast from source type to destination type.
This is the code that gives the error:
public Java.Lang.Object [] GetSections ()
{
var intPtr = JNIEnv.NewArray (sections.ToArray ());
var array = new JavaArray<Java.Lang.Object> (intPtr, JniHandleOwnership.TransferLocalRef);
return (Java.Lang.Object []) array;
}
How can I fix this?
You're casting JavaArray<Java.Lang.Object>
to Java.Lang.Object []
.
Change the return type of GetSessions()
to JavaArray<Java.Lang.Object>
public JavaArray<Java.Lang.Object> GetSections ()
{
var intPtr = JNIEnv.NewArray (sections.ToArray ());
return new JavaArray<Java.Lang.Object> (intPtr, JniHandleOwnership.TransferLocalRef);
}
Your second problem probably means that you have implemented a interfacit wich GetSections method needs a return type of Java.Lang.Object.
If you made your interface yourself you probably made the same mistake there as what you did in this method.
So:
public JavaArray<Java.Lang.Object> GetSections ()
{
var intPtr = JNIEnv.NewArray (sections.ToArray ());
return new JavaArray<Java.Lang.Object> (intPtr, JniHandleOwnership.TransferLocalRef);
}
and your interface should look like this:
public JavaArray<Java.Lang.Object> GetSections ();
i think you made the same mistake twice
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.