基本上,我需要在片段中使用 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);
我的问题是:如何使用此代码/获得相同的结果,以便可以在我的片段中使用此代码?