Android中的MultiThread for Loop和Delay

时间:2015-11-09 14:11:47

标签: android multithreading

正如你所看到的,我必须做TASK1并且不断进行TASK2。在1到2之间,他们有延迟。然后我想无限地重复它,直到活动结束。

我对线程没有经验..我无法修复此错误。 帮助我!

      Timer timer = new Timer();
      timer.schedule(new TimerTask() {

          @Override
          public void run() {
          //TASK 1
          main_1.setVisibility(View.VISIBLE);
          main_2.setVisibility(View.INVISIBLE);
          main_1.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));


                Handler mHandler = new Handler(Looper.getMainLooper());
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                    //TASK 2
                    main_1.setVisibility(View.INVISIBLE);
                    main_2.setVisibility(View.VISIBLE);
                    main_2.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));

                    }
                }, 1000);
                }
            }, 1000, 1000);

这是我的错误

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

2 个答案:

答案 0 :(得分:1)

 Timer timer = new Timer();
      timer.schedule(new TimerTask() { 

          @Override 
          public void run() { 
          //TASK 1 


         YourActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
 main_1.setVisibility(View.VISIBLE);
          main_2.setVisibility(View.INVISIBLE);
          main_1.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
            }
        });

                Handler mHandler = new Handler(Looper.getMainLooper());
                mHandler.postDelayed(new Runnable() {
                    @Override 
                    public void run() { 
                    //TASK 2 
        YourActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                  main_1.setVisibility(View.INVISIBLE);
                    main_2.setVisibility(View.VISIBLE);
                    main_2.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
            }
        });


                    } 
                }, 1000); 
                } 
            }, 1000, 1000); 

答案 1 :(得分:0)

计时器在后台线程上运行。您无法从后台线程更新ui,因此请始终使用runOnUiThread来实现此目的。

  Timer timer = new Timer();
  timer.schedule(new TimerTask() {

      @Override
      public void run() {
            YourActivity.this.runOnUiThread(new Runnable() {

              @Override
              public void run() {
                    main_1.setVisibility(View.VISIBLE);
                    main_2.setVisibility(View.INVISIBLE);              
                    main_1.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
               }
            });

            Handler mHandler = new Handler(Looper.getMainLooper());
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                //TASK 2
                main_1.setVisibility(View.INVISIBLE);
                main_2.setVisibility(View.VISIBLE);
                main_2.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));

                }
            }, 1000);
            }
        }, 1000, 1000);