2

My application receives an intent from a service which then pops up a notification. This intent has an object id from which I want to retrieve the whole object from my database so that I can set the notification title and subtitle. However, I need to pass in a context to my dbManager, and I have been receiving errors no matter which context I try.

The code is as follows:

public class ReceiveTransitionsIntentService extends IntentService {

    private DBManager dbManager;
    public ReceiveTransitionsIntentService() {
        super("ReceiveTransitionsIntentService");
    }
    protected void onHandleIntent(Intent intent) {
        //somewhere
        sendNotification(..);
    }
    private void sendNotification(String transitionType, String ids) {
         Intent notificationIntent =
                new Intent(getApplicationContext(),MainActivity.class);
         ...
         //have tried with getbaseContext() and this, still failures
         dbManager = new DBManager(getApplicationContext());
         ...
    }
}

Does anyone have any ideas on what I could do?

These are the errors:

new DBManager(this) -> java.lang.ClassCastException: .ReceiveTransitionsIntentService cannot be cast to android.app.Activity

new DBManager(getApplicationContext) -> java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity

new DBManager(getBaseContext) -> java.lang.ClassCastException: android.app.Contextimpl cannot be cast to android.app.activity

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
gta0004
  • 508
  • 4
  • 11
  • 29

1 Answers1

1

you are getting the following:

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


new DBManager(getApplicationContext) -> 
java.lang.ClassCastException: android.app.Application 
cannot be cast to android.app.Activity

new DBManager(getBaseContext) -> 
java.lang.ClassCastException: 
android.app.Contextimpl cannot be cast to android.app.activity

This simply means you are receiving Activity in the parameters. that is your function signature contains:

functionName(Activity context); 

Change the functions to:

functionName(Context context), thus when you pass context, as `this` 

or someOtherActivity.this or getApplicationContext() it will not lead to the errors.

gta0004
  • 508
  • 4
  • 11
  • 29
  • Oh yeah in my DBManager I cast the context to Activity since I need to activity also. Guess I need to fix that. Failing at reading the error log. – gta0004 Feb 14 '14 at 04:34
  • And if you are sure which activity is calling the method, you could typecast it as ((Activity)context) if at all its really needed. – Rat-a-tat-a-tat Ratatouille Feb 14 '14 at 04:37
  • Yeah I had `mActivity = (Activity) mContext` in DBManager which I guess wouldn't work when it wasn't my activity called it. I changed my class to use context and it worked so hopefully that does not come back to haunt me later on. Thanks for your help! – gta0004 Feb 14 '14 at 04:47