0

I have a broadcast receiver that starts a service when a gcm message is received. But I get an exception saying :

Logs:

trace->java.lang.RuntimeException: Unable to start receiver         com.xyz.ab.gcm.GcmBroadcastReceiver: java.lang.ClassCastException:   com.xyz.ab.gcm.ServiceWrapperContext cannot be cast to  android.app.ContextImpl

 03-19 13:09:11.558: E/(8913): MainApplication: onCreate: uncaughtException:            
thread->mainException was: Unable to start receiver 
com.xyz.ab.gcm.GcmBroadcastReceiver: java.lang.ClassCastException:   
com.xyz.ab.ServiceWrapperContext cannot be cast to android.app.ContextImpl 
stack trace->java.lang.RuntimeException: Unable to start receiver 
com.xyz.ab.gcm.GcmBroadcastReceiver: java.lang.ClassCastException: 
com.xyz.ab.ServiceWrapperContext cannot be cast to android.app.ContextImpl
03-19 13:09:11.558: E/(8913):  at    
android.app.ActivityThread.handleReceiver(ActivityThread.java:2520)
03-19 13:09:11.558: E/(8913):  at 
android.app.ActivityThread.access$1500(ActivityThread.java:156)
03-19 13:09:11.558: E/(8913):  at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1416)
03-19 13:09:11.558: E/(8913):  at   
android.os.Handler.dispatchMessage(Handler.java:99) 
03-19 13:09:11.558: E/(8913):  at android.os.Looper.loop(Looper.java:153)
03-19 13:09:11.558: E/(8913):  at   
android.app.ActivityThread.main(ActivityThread.java:5297)
03-19 13:09:11.558: E/(8913):  at  
java.lang.reflect.Method.invokeNative(Native Method)
03-19 13:09:11.558: E/(8913):  at 
java.lang.reflect.Method.invoke(Method.java:511)
03-19 13:09:11.558: E/(8913):  at 
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
03-19 13:09:11.558: E/(8913):  at  
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-19 13:09:11.558: E/(8913):  at dalvik.system.NativeStart.main(Native 
Method)
03-19 13:09:11.558: E/(8913): Caused by: java.lang.ClassCastException:  
com.xyz.ab.ServiceWrapperContext cannot be cast to android.app.ContextImpl
03-19 13:09:11.558: E/(8913):  at      
android.app.ActivityThread.handleReceiver(ActivityThread.java:2501)

Code causing issue:

 protected void attachBaseContext( Context base ) {
 ServiceWrapperContext context = new ServiceWrapperContext( base );
 context.addCallback( Context.LAYOUT_INFLATER_SERVICE,
            new TypefaceLayoutInflater.ServiceCallback( ) );
    super.attachBaseContext( context );

The ServiceWrapperContext extends ContextWrapper.

When I comment the above method it works fine else gives the above exception.

Please help as my colleague had made these changes. I tried few things but its not working. Not able to understand the issue here.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2234
  • 1,282
  • 1
  • 21
  • 44

2 Answers2

2

I am pretty sure the following line causes the problem:

super.attachBaseContext( context );

You are passing an instance of ServiceWrapperContext class, instead of Context class. I am not sure about your class hierarchy, but maybe you should try calling:

super.attachBaseContext( base );
2

android.app.ContextImpl is the base class that all other "base contexts" of activities must derive and is a subclass of the android.content.Context class. ContextWrapper is also a subclass of android.content.Context.

Obviously, you can't have a class that subclasses 2 other classes in java, but you have to provide a ContextImpl as the base class. (Based on ContextImpl docs, note that even though the method requires a Context the Activity or BroadcastReciever assumes it is a ContextImpl).

The reason why your colleague used ContextWrapper instead is because they wanted to add functionality to the base context without altering whatever functionality it already has. ContextWrapper delegate all its Context implementations by calling the implementation of its base context, but since the activity expects additional functionalities (in the form of ContextImpl) this will not work here. You can either create a ContextImplWrapper that does with ContextImpl objects what ContextWrapper does with Context objects, or implement what the ServiceWrapperContext does in some other way.

Community
  • 1
  • 1
SilverCorvus
  • 2,956
  • 1
  • 15
  • 26