Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not serialize ValueFormatter interface member variable #920

Merged
merged 1 commit into from
Aug 3, 2015

Conversation

dakotasanchez
Copy link
Contributor

When deserializing an object from this lib that contains a ValueFormatter member variable, Gson throws an exception. And since there is a ValueFormatter variable in DataSet, and ChartData uses DataSet, this affects many objects. I came across it deserializing my object which contained a PieChart using mGson.fromJson(myJsonString, MyPieChartObject.class);. Making an instance creator as Gson suggested did not solve the problem.

To fix this all I did was make the mValueFormatter transient so it does not get serialized by Gson, and therefore does not throw the exception. It works like a charm, and hopefully shouldn't affect anything negatively.

Before:
(Stack trace)

1265-1265/com.example.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.test, PID: 1265
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.DetailActivity}: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.github.mikephil.charting.utils.ValueFormatter. Register an InstanceCreator with Gson for this type may fix this problem.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.github.mikephil.charting.utils.ValueFormatter. Register an InstanceCreator with Gson for this type may fix this problem.
            at com.google.gson.internal.ConstructorConstructor$12.construct(ConstructorConstructor.java:210)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:186)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
            at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
            at com.google.gson.Gson.fromJson(Gson.java:810)
            at com.google.gson.Gson.fromJson(Gson.java:775)
            at com.google.gson.Gson.fromJson(Gson.java:724)
            at com.google.gson.Gson.fromJson(Gson.java:696)
            ...

After:
No exceptions for deserializing any of my objects that contain a chart from this lib anymore!

PhilJay added a commit that referenced this pull request Aug 3, 2015
Do not serialize ValueFormatter interface member variable
@PhilJay PhilJay merged commit 62f192e into PhilJay:master Aug 3, 2015
regas99 pushed a commit to regas99/MPAndroidChart that referenced this pull request Apr 1, 2019
Do not serialize ValueFormatter interface member variable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants