Skip to content Skip to sidebar Skip to footer

Mpandroidchart Entry Cannot Be Cast To Barentry

I want to create a BarChart using MPAndroidChart but I get the following error java.lang.ClassCastException: com.github.mikephil.charting.data.Entry cannot be cast to com.github.mi

Solution 1:

Using MPAndroidChart 3.0.1

I'm sorry to say this looks like an incomplete implementation of BarEntry. If you inspect the source, you can see the following:

  1. BarEntry extends Entry
  2. Entry implements Parcelable
  3. BarEntry doesn't have its own Parcelable<Creator>

So when you serialise into a parcel, and then deserialise, you'll get a List<Entry> instead of List<BarEntry> :-)

For now there is nothing you can do apart from work around this.

You can take the opportunity to refactor to achieve a better separation of layers. BarEntry is a member of the view layer or the view model layer. Instead of passing around BarEntry, you should probably have a clear model layer which is a mere data object. You can pass lists or arrays of this model layer around in an Intent easily and the consumers can convert to BarEntry as necessary.

Alternatively, create an abstraction of a data source. Something like a TemperatureRepository class. Then Activitys and Fragments that consume this data can get the data from the repository rather than from an Intent.

Post a Comment for "Mpandroidchart Entry Cannot Be Cast To Barentry"