Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class cast exception to same class on Android

I'm having a strange problem with ClassCastException on Android. One class cannot be casted to the same class:

java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.ClassCastException: com.example.model.BadeWrapper cannot be cast to com.example.model.BadgeWrapper

java.lang.ClassCastException: com.example.events.widgets.TouchyWebView cannot be cast to com.example.events.widgets.TouchyWebView

java.lang.ClassCastException: com.example.friends.widgets.FriendsTabView cannot be cast to com.example.friends.widgets.FriendsTabView

When I find the line with error, all it does is finding view by id or creating fragment with arguments e.g.:

FriendsTabView friendsTabView;
friendsTabView = (FriendsTabView) view.findViewById(R.id.friends_bottom_tab_panel);

As my BugSense tells, this problem occurs only on Samsung Galaxy S5 with android 5.0.0 (samsung SM-G900F). I have never met this problem on my other devices:

  • Motorola Moto G 1st gen (Android 5.0.1)
  • Samsung Galaxy S3 Mini (Android 4.1.2)
  • LG G2 Mini (Android 4.4.2)
  • Sony Xperia L (Android 4.1.2)

Anyone met this problem before? Is there any way to fix it?

like image 907
rwojcik Avatar asked Mar 12 '15 10:03

rwojcik


People also ask

What causes a class cast exception?

Introduction. ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

How do you resolve ClassCastException?

// type cast an parent type to its child type. In order to deal with ClassCastException be careful that when you're trying to typecast an object of a class into another class ensure that the new type belongs to one of its parent classes or do not try to typecast a parent object to its child type.

Can we catch ClassCastException?

You would get a ClassCastException, but not whithin the cast-method, but at the assignment to s. But this will still lead to an error later, when the you use your class.

Is ClassCastException a checked exception?

ClassCastException is one of the unchecked exception in Java. It can occur in our program when we tried to convert an object of one class type into an object of another class type.


1 Answers

Ok I where the problem is. Looks like Samsung on Android 5.0 probably still uses old implementation of multidex (used in Android L Preview), so if your app exceeds the 64k limit, it will crash in random places on your app where you use (ExampleClass) object cast.

More info on this problem can be found here and here.

We're also seeing this crash happen thousands of times per day. Crashlytics reports the crash as 100% Samsung devices (99% "SM G900F[Q]" device), and 100% Android 5.x. Seems to be another multidex issue on Samsung + Lollipop devices, as a.jaskev reports in #3.

Looks like we have to wait until Samsung resolve this problem. Right now all we can do is watching our spam on mailbox with bug reports :)

like image 62
rwojcik Avatar answered Oct 19 '22 05:10

rwojcik