【问题标题】:Change SnackBar BackgroundColor EventBus Exception更改 SnackBar BackgroundColor EventBus 异常
【发布时间】:2017-08-28 22:23:33
【问题描述】:

activity从一个Fragment接收事件,activity需要创建一个snackbar并改变snackbar的背景。 但这样做时,日志中的错误如下所示:

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

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

【问题讨论】:

    标签: android greenrobot-eventbus greenrobot-eventbus-3.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

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 2020-10-28
      • 2023-04-02
      • 2018-05-07
      • 1970-01-01
      相关资源
      最近更新 更多