获取应用程序对象时出现ClassCastException

时间:2017-06-07 18:04:11

标签: android firebase firebase-cloud-messaging dagger-2

最近我在应用程序中做了一些更改,因为静态字段存在问题。在Google Play商店中更新我的应用后,我现在看到一些随机的(在Android Vitals中,Fabric没有报告它们)崩溃,与ClassCastException连接如下:

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)

当收到推送通知时,它主要发生在这个地方。

当然,我的清单中声明了完整的包名:

<?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">

我的带有onMessageReceived的CustomFirebaseMessagingService看起来像这样:

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);

最后一行是发生ClassCastException的地方。

这也是我的CustomApplication代码的片段:

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;
    }
}

我猜,它与Android生命周期有关,也许系统会杀死我的应用,但为什么之后getApplication()无法投放到我的CustomApplication类?

我在MyCustomFragment.onAttach()方法中也看到了这个bug,我这样做了:

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

我不知道该如何处理:/

提前致谢

0 个答案:

没有答案