locked
JNIEnv.GetArray<Java.Lang.Object> return InvalidCastException: Cannot cast from source type to desti RRS feed

  • Question

  • User29680 posted

    Hello! I am using Android L Developer Preview on Nexus 5. (API 21). My app has Sms Broadcast Receiver, in which i try to read incoming sms message. In OnReceive method:

      var bundle = intent.Extras();
        if (bundle == null)
                return null;
        string address = "";
        string smsBody = "";
        Java.Lang.Object pdus = bundle.Get("pdus");
        Java.Lang.Object[] castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
        SmsMessage[] msgs = new SmsMessage[castedPdus.Length];
                for (var i = 0; i < msgs.Length; i++)
                {
                    byte[] bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
                    JNIEnv.CopyArray(castedPdus[i].Handle, bytes);
                    msgs[i] = SmsMessage.CreateFromPdu(bytes);
                    smsBody += msgs[i].MessageBody;
                }
                address = msgs[0].OriginatingAddress;
    

    string of code:

    Java.Lang.Object[] castedPdus = JNIEnv.GetArray(pdus.Handle)

    return InvalidCastException; Exception StackTrace:

    [InvalidCastException: Cannot cast from source type to destination type.] (wrapper castclass) object:castclasswithcache (object,intptr,intptr) Android.Runtime.JNIEnv.CopyArray[Object](IntPtr src, Java.Lang.Object[] dest):0 Android.Runtime.JNIEnv.GetArray[Object](IntPtr arrayptr):0 ReadSmsApplication.Droid.SmsReceiver.GetSmsFromMessage(Android.OS.Bundle bundle):0 ReadSmsApplication.Droid.SmsReceiver.OnReceive(Android.Content.Context context, Android.Content.Intent intent):0 Android.Content.BroadcastReceiver.nOnReceiveLandroidcontentContextLandroidcontentIntent(IntPtr jnienv, IntPtr nativethis, IntPtr nativecontext, IntPtr native_intent):0 (wrapper dynamic-method) object:d10708fc-3178-4b7a-b2ee-7dfdac3f28bc (intptr,intptr,intptr,intptr)

    In android 4.4.2 on Samsung Galaxy S4 all work fine!

    Wednesday, October 22, 2014 9:09 AM

All replies

  • User97483 posted

    I have this exact same issue. I've not been able to figure it out and nobody over at StackOverflow ( http://stackoverflow.com/questions/27864321/cannot-cast-from-source-type-to-destination-type-xamarin-studio ) has been able to help with this issue either. OP: Do you have any updates on this? Did you manage to figure it out? If so could you please share any updates you may have?

    Saturday, January 10, 2015 11:01 PM
  • User29680 posted

    Check this thread on xamarin bugzilla: http://bugzilla.xamarin.com/show_bug.cgi?id=26074

    Thanks to Jonathan Pryor for bug fix. Waiting for updates.

    Thomas Van Machelen suggested workaround, that works fine! But only for sms with one message. If you want to parse messages that containing more sms, try something like this:

    string smsBody; string address;

    Java.Lang.Object rawPdus = bundle.Get("pdus"); int length = JNIEnv.GetArrayLength(pdus.Handle);

    if (length < 1) return null;

    byte[] bytes = new byte[length]; SmsMessage[] msgs = new SmsMessage[length]; for (int i = 0; i < length; i++) { byte[] pdus = JNIEnv.GetArrayItem(rawPdus.Handle, i); msgs[i] = SmsMessage.CreateFromPdu(pdus); smsBody += msgs[i].MessageBody; } address = msgs[0].OriginatingAddress;

    Thanks to Thomas Van Machelen.

    Friday, January 30, 2015 10:32 PM
  • User15788 posted

    In the meantime, the issue has been reported (not by me) in the Xamarin Bugzilla https://bugzilla.xamarin.com/show_bug.cgi?id=26074

    Monday, March 9, 2015 8:22 PM
  • User15788 posted

    In the meantime, the issue has been reported (not by me) in the Xamarin bugzilla https://bugzilla.xamarin.com/show_bug.cgi?id=26074

    Monday, March 9, 2015 8:22 PM