I am developing an app in Xamarin
Android. I have a splash screen where I serialize a class and pass it to MainActivity
using Intent
. When I try to deserialize it in MainActivity
I get an error message :
"SerializationException unable to find constructor to use for types"
Serialization :
void LoadData() {
Currency currency = new Currency(this);
intent = new Intent(this, typeof(MainActivity));
intent.PutExtra("currency", Newtonsoft.Json.JsonConvert.SerializeObject(currency));
StartActivity(intent);
}
Deserialization :
cur = Newtonsoft.Json.JsonConvert.DeserializeObject<Currency> (Intent.GetStringExtra("currency")); // I get the error here
Class constructor :
public Currency(Context context) {
thiscontext = context;
}
I began experiencing this problem after I added the parameter Context context
to the constructor.
What has caused this? How can I solve it?
Is it possible to serialize/deserialize class which has parameters?
One other thing might cause this issue is if you have a full linking enabled in your android project. you need to preserve class and members using
[Runtime.Preserve(AllMembers = true)]
public class Currency
{}
You need to have an empty constructor in Currency class in order to deserialize it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With