Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java.IO.ISerializable Xamarin

I'm trying to pass object in Android using Iserializable, but it return me "Unable to activate instance of type from native handle" exception.

Below are my codes.

[FBUserParcel.cs]

using System;
using Android.OS;
using PlayCardLeh.Helpers;

    namespace PlayCardLeh.Android
    {
        public class FBUserParcel:Java.Lang.Object, Java.IO.ISerializable
        {
                Xamarin.Facebook.Model.IGraphUser fbUser;
                public FBUserParcel (Xamarin.Facebook.Model.IGraphUser user)
                {
                    fbUser = user;

                }
            }
    } 

[MainActivity.cs]

private async Task FindFBUser(Xamarin.Facebook.Model.IGraphUser user) {
    if (user != null) {
        Console.WriteLine ("GOT USER: " + user.Name);
        try {
            var t = await User.FindUserWithFBID (user);
        }
        catch(NotFound NotFound) {

            RunOnUiThread (() => {
                Intent i = new Intent (this,typeof (RegisterActivity));
                FBUserParcel u = new FBUserParcel(user);
                StartActivity (i);
            });

        }

    }

    else
        Console.WriteLine ("Failed to get 'me'!");
}

RegisterActivity.cs

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    SetContentView (Resource.Layout.Register);

    if (Intent.HasExtra ("fbUser")) {
        var u = Intent.Extras;
        u.GetSerializable ("fbUser");

    }
}

The exception happen at "u.GetSerializable ("fbUser");". Sorry I'm kinda new to Xamarin, anyone have exprience with using Iserializable? What did I missed out in "FBUserParcel.cs" ??

Thanks Mug4n for sharing a link that discuss about the same issue. http://forums.xamarin.com/discussion/451/communicate-with-iserializable

like image 480
Chris Sim Avatar asked Nov 30 '14 06:11

Chris Sim


1 Answers

Well you could solve it easily by just serializing Xamarin.Facebook.Model.IGraphUser with any serializer which produces string or byte[].
For example if you use Newtonsoft's Json.NET component your code would look something like that:

Xamarin.Facebook.Model.IGraphUser fbUser;
var fbUserSerialized = JsonConvert.SerializeObject (fbUser);
intent.PutExtra ("fbUser");

And to deserialize:

if (Intent.HasExtra ("fbUser")) {
    var fbUserSerialized = Intent.GetStringExtra ("fbUser");
    var fbUser = JsonConvert.DeserializeObject<Xamarin.Facebook.Model.IGraphUser>(fbUserSerialized);
}
like image 77
Alex.F Avatar answered Nov 02 '22 06:11

Alex.F