ClassCastException when getting an application object

I recently made some changes to my application due to a problem with static fields. After updating my app on the Google Play Store, I now see several random (in Android Vitals, Fabric did not destroy them) crashes related to ClassCastException, for example:

java.lang.ClassCastException: 
  at pl.myapp.myapp_name.notification.CustomFirebaseMessagingService.onMessageReceived(CustomFirebaseMessagingService.java:86)
  at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(FirebaseMessagingService.java:0)
  at <OR>.zzZ(FirebaseMessagingService.java:0)
  at com.google.firebase.iid.zzb$1.run(zzb.java:0)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
  at java.lang.Thread.run(Thread.java:818)

      

it happens mostly at this location while the push notification is received.

Of course, I have the fully qualified package name declared in my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="pl.myapp.myapp_name"
    android:installLocation="auto">

    <uses-permission ... />

    <application
        android:name="pl.myapp.myapp_name.CustomApplication"
        android:largeHeap="true"
        android:allowBackup="true"
        android:icon="${appIcon}"
        android:label="@string/application_name"
        android:theme="@style/ApplicationThemeLightBackground"
        tools:replace="android:label">

      

and my CustomFirebaseMessagingService with onMessageReceived looks something like this:

package pl.myapp.myapp_name.notification;

import (..)

public class CustomFirebaseMessagingService extends FirebaseMessagingService {

    @Inject
    SettingsService settingsService;

    @Inject
    EventBus bus;

    @Inject
    protected AnalyticsHelper analyticsHelper;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        ((CustomApplication)getApplication()).getComponent().inject(this);

      

The last line is where the ClassCastException occurs.

Here is also a snippet of my CustomApplication code:

package pl.myapp.myapp_name;

import (..)
public class CustomApplication extends Application {

    private MyApplicationComponent mApplicationComponent;

    public CustomApplication() {
    }

    public CustomApplication(final Context context) {
        this();
        attachBaseContext(context);
    }

    public void initializeComponent() {
        mApplicationComponent = DaggerCustomApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .netModule(new NetModule())
                .dataModule(new DataModule())
                .build();
        mApplicationComponent.inject(this);
    }

    public MyApplicationComponent getComponent() {
        if (mApplicationComponent == null) {
            initializeComponent();
        }
        return mApplicationComponent;
    }
}

      

I guess it has something to do with the Android lifecycle, maybe the system is killing my app, but why can't getApplication () be sent to my CustomApplication class after that?

I saw this error also in the MyCustomFragment.onAttach () method where I am doing this:

((CustomApplication)getActivity().getApplication()).getComponent().inject(this);

      

I don't know what to do about it: /

Thank you in advance

+3


source to share





All Articles