更改 SnackBar BackgroundColor EventBus 异常

Change SnackBar BackgroundColor EventBus Exception

activity从一个Fragment接收到事件,activity需要创建一个snackbar并改变snackbar的背景。 但是在这样做的过程中,日志中的错误是这样发生的:

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

接收Fragment事件的方法是这样的:

@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();
    }

错误线 mSnackbar.getView().setBackgroundColor(ContextCompat.getColor(this, color));

您似乎在调用 ContextCompat.getColor() 两次:

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

只需将对setBackgroundColor()的调用更改为直接使用color