0

基本上,我需要在片段中使用 TimerTask 但出现此错误:

04-04 15:01:02.710 28397-28528/com.example.sdilab.pap E/AndroidRuntime: FATAL EXCEPTION: Timer-1
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5471)
    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1007)
    at android.view.View.requestLayout(View.java:15850)
    at android.view.View.requestLayout(View.java:15850)
    at android.view.View.requestLayout(View.java:15850)
    at android.view.View.requestLayout(View.java:15850)
    at android.view.View.requestLayout(View.java:15850)
    at android.view.View.requestLayout(View.java:15850)
    at android.support.v4.widget.DrawerLayout.requestLayout(DrawerLayout.java:1255)
    at android.view.View.requestLayout(View.java:15850)
    at android.view.View.requestLayout(View.java:15850)
    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:334)
    at android.view.View.requestLayout(View.java:15850)
    at android.view.View.requestLayout(View.java:15850)
    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:334)
    at android.view.View.requestLayout(View.java:15850)
    at android.view.View.requestLayout(View.java:15850)
    at android.widget.TextView.checkForRelayout(TextView.java:6615)
    at android.widget.TextView.setText(TextView.java:3753)
    at android.widget.TextView.setText(TextView.java:3608)
    at android.widget.TextView.setText(TextView.java:3583)
    at com.example.sdilab.pap.StepsFragment$1.run(StepsFragment.java:87)
    at java.util.Timer$TimerImpl.run(Timer.java:284)

这是我需要使用的代码:

timer.schedule( new TimerTask() {
            public void run() {
                double value = Double.parseDouble(readFromFile());
                String roundedvalue = Math.round(passos) + "";
                textView.setText(roundedvalue);

            }
        }, 0, 1*1000);

我的问题是:如何使用此代码/获得相同的结果,以便可以在我的片段中使用此代码?

4

3 回答 3

1

使用runOnUiThread()方法更新TextView

timer.schedule( new TimerTask() {
                public void run() {
                    double value = Double.parseDouble(readFromFile());
                    String roundedvalue = Math.round(passos) + "";

         runOnUiThread(new Runnable() {
                public void run() {
                   textView.setText(roundedvalue);  
                 }
             });



                }
            }, 0, 1*1000);
于 2016-04-04T14:07:58.210 回答
0

您也可以使用此代码。这应该在onCreateView方法中完成。例如,我希望 ViewPager 自动移动到下一张幻灯片,等待 6 秒然后滑动到下一个内容。

//Create a new Timer and Handler object
Timer timer = new Timer;
Handler updateSlide = new Handler();
private static int delay = 300; //delay before moving to the next slide.
private static int wait = 6000;
timer.schedule(new TimerTask (){
   @Override
   public void run (){
      //gets the list, and puts the last into the front
      updateSlide.postAtFrontOfQueue(new Runnable(){ 

       @Override
       public void run (){

         if(viewPager.getCurrentItem() < sliderList.size() -1){

            viewPager.setCurrentItem(viewPager.getCurrentItem() +1);

         }
         else{

              viewPager.setCurrentItem(0);

         }

       }

      });
   }
}, delay, wait);

有了这个,我希望你能理解。此代码使您有权使用 Handler() 在 Fragment 中使用 TimerTask。在一个 Activity 中,使用 MainActivity.runOnUiThread(nee Runnable().... Works 非常好。

所以我建议你在你的片段中使用上面的代码示例。谢谢。

于 2020-05-18T01:44:43.623 回答
0

您的问题是您试图在主线程之外更新您的视图。处理此问题的一种简单方法是使用处理程序来确保您以正确的方式更新视图。这是一个例子:

private Handler updateHandler = new Handler();

timer.schedule( new TimerTask() {
        public void run() {
            double value = Double.parseDouble(readFromFile());
            String roundedvalue = Math.round(passos) + "";
            updateHandler.postRunnable(new Runnable() {
                public void run() {
                    textView.setText(roundedvalue); 
                }
            });
        }
    }, 0, 1*1000);

还有一种runOnUiThread()方法,如果你在一个活动中,它会做同样的事情。

于 2016-04-04T14:11:23.537 回答