Solved: java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity

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: android.app.ContextImpl cannot be cast to android.app.Activity

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.

Follow Lynxbee

Leave a Reply / Ask Question