Android RxJava引起ConcurrentModificationException

时间:2018-11-15 19:56:32

标签: java android rx-java

我是Android开发中RxJava的新手。我在项目AsyncTask中将其更改为RxJava,并获得了ConcurrentModificationException。是的,我使用了集合(sparseArray),但这并不重要,因为在findViewById.setVisibility中抛出了异常。仅当我尝试调用setVisibility时。我很困惑,我做错了什么?我在片段中有一个TextView。首先,我设置了OnClickListener,在侦听器中,我初始化了Single.fromCallable,然后设置了OnDragListener

TextView tv;

tv.setOnClickListener(v -> {
          if (isClickEnable) {
              tv.setBackgroundResource(R.drawable.cheap_dark);
              cheapInObservable(tv);

          }

});

tv.setOnDragListener(new MyDragListener());

private void cheapInObservable(TextView tView) {
        Single.fromCallable( () ->  tView).subscribeOn(Schedulers.io())
              .delay(250, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
              .doOnSuccess(this::onSuccessCheapIn)
              .subscribe();
    }

在这段代码中,我得到了证明:

private class MyDragListener implements View.OnDragListener {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            View dragView = (View)event.getLocalState();
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_ENDED:
                    if(!event.getResult()) {
                        if(dragView == v) {
                            dragView.setVisibility(View.VISIBLE);
                            activity.findViewById(sparseCheaps.get(dragView.getId())).
                                     setVisibility(View.VISIBLE);

                        }
                    }
                    break;


private void onSuccessCheapIn(TextView tv) {
            tv.setVisibility(View.INVISIBLE);
            TextView tvd = activity.findViewById(sparseCheaps.get(tv.getId()));
            tvd.setVisibility(View.VISIBLE);
            AnimatorSet set = new AnimatorSet();
            tvd.animate().rotation(0);
            int up = activity.findViewById(R.id.guidelineGlowUp).getTop();
            int left = getXcoord(tvd);
            set.setDuration(400).playTogether(ObjectAnimator.ofFloat(tvd, TextView.TRANSLATION_X,
                    tvd.getX(), left),
                    ObjectAnimator.ofFloat(tvd, TextView.TRANSLATION_Y, tvd.getY(), up - 3));

            set.setInterpolator(new AccelerateInterpolator((float) 0.4));

            set.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    isAnimationCheaps = false;
                    super.onAnimationEnd(animation);
                }
            });

            set.start();
        }

我发现使用setVisibility时仅抛出异常。如果我使用AsyncTask而不是Rx,它将毫无例外地工作

StackTrace是:

java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:795)
        at java.util.HashMap$KeyIterator.next(HashMap.java:822)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1154)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
        at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:4322)
        at android.view.ViewRootImpl.access$1100(ViewRootImpl.java:103)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3407)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5370)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案