Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top