Search code examples
androidgreenrobot-eventbusgreenrobot-eventbus-3.0

Change SnackBar BackgroundColor EventBus Exception


The activity receives the event from a Fragment, the activity needs to create a snackbar and change the background of the snackbar. But in doing so the error in the log happens like this:

08-28 16:15:58.233 13491-13491 E/EventBus: Could not dispatch event: class SearchPickingListEvent to subscribing class class HomeActivity android.content.res.Resources$NotFoundException: Resource ID #0xffffce00

The method that receives the Fragment event is like this:

@Subscribe(threadMode = ThreadMode.MAIN)
    public void onSearchListEvent( SearchListEvent searchListEvent) {
        if (searchListEvent.isSuccess()) {
            loadingEventsUI(getString(R.string.findingPickingList), Snackbar.LENGTH_INDEFINITE,
                    ContextCompat.getColor(this, R.color.snackbar_background_warning));

                    controller.fetchInvoice(searchListEvent.getCdCode(),
                    searchListEvent.getPickingListNumber());
        } else {
            showMessage(R.string.orderinvalid);
        }
    }



private void loadingEventsUI(String message, int duration, int color){
        mSmoothProgressBar.progressiveStart();
        mSmoothProgressBar.setVisibility(View.VISIBLE);
        mSnackbar = Snackbar.make(btnMainMenu, message, duration);
        mSnackbar.getView().setBackgroundColor(ContextCompat.getColor(this, color));
        mSnackbar.show();
    }

Erro line mSnackbar.getView().setBackgroundColor(ContextCompat.getColor(this, color));


Solution

  • Looks like you're invoking ContextCompat.getColor() twice:

    public void onSearchListEvent( SearchListEvent searchListEvent) {
        ...
        loadingEventsUI([message], [duration], ContextCompat.getColor(this, R.color.snackbar_background_warning));
        ...
    }
    
    private void loadingEventsUI(String message, int duration, int color){
        ...
        mSnackbar.getView().setBackgroundColor(ContextCompat.getColor(this, color));
        ...
    }
    

    Simply change the call to setBackgroundColor() to directly use color.