Solved: java.lang.ClassCastException: cannot be cast to

If you are working on Android application or AAR Library, you will be using applications context frequently for accessing android system API’s.While using those API’s which are dependent on Context, there are chances you may see an error like as below,

System.err: java.lang.ClassCastException: cannot be cast to

Solution : When we encountered this error in to accessing one of our library, we were passing Context to library API using getBaseContext() API whereas our library was expecting application context, hence solution which worked was,

Change getBaseContext() to “this” while calling the library function.

Leave a Comment