更改SnackBar BackgroundColor EventBus异常

时间:2017-08-28 22:23:34

标签: android greenrobot-eventbus greenrobot-eventbus-3.0

活动从片段接收​​事件,活动需要创建快餐栏并更改快餐栏的背景。 但是这样做会导致日志中出现错误:

  

08-28 16:15:58.233 13491-13491 E / EventBus:无法调度事件:类SearchPickingListEvent订阅类类HomeActivity   android.content.res.Resources $ NotFoundException:资源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();
    }

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

1 个答案:

答案 0 :(得分:1)

您似乎在两次调用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