Problem with Simple Timer / Timertask for Android

I am trying to get a basic timer for working on Android, and I have the following code in the onCreate () method:

editText = (EditText) findViewById(R.id.edit_message); Timer time = new Timer(); time.schedule(new TimerTask() { public void run() { timesince++; editText.setText("Timeran:"+timesince); Log.d("TIMER", "TimerTask run"); }}, 0, 1000); 

So, hopefully, it will update the text box with an increase in the value (timesince), which is declared at the top of the program, along with editText ...

However (although I see no problems with compilation), when I run it on the emulator, it does not work at all. The appendix says: "blah blabblah stopped working ..."

Am I doing timers wrong? What am I missing?

Thanks a lot.

(The P. application works fine if this code is removed, so I know there is a problem in this section.)

EDIT: I believe this is my logcat output, sorry for the text wall:

 10-11 03:40:49.260: E/AndroidRuntime(561): FATAL EXCEPTION: Timer-0 10-11 03:40:49.260: E/AndroidRuntime(561): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 10-11 03:40:49.260: E/AndroidRuntime(561): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:3939) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:714) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:763) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4012) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.view.View.invalidate(View.java:8454) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.widget.TextView.invalidateCursor(TextView.java:4319) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.widget.TextView.spanChange(TextView.java:7670) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:8020) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:898) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:614) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:520) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.text.Selection.setSelection(Selection.java:76) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.text.Selection.setSelection(Selection.java:87) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.widget.TextView.setText(TextView.java:3244) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.widget.TextView.setText(TextView.java:3110) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.widget.EditText.setText(EditText.java:78) 10-11 03:40:49.260: E/AndroidRuntime(561): at android.widget.TextView.setText(TextView.java:3085) 10-11 03:40:49.260: E/AndroidRuntime(561): at com.example.cultivation.MainActivity$1.run(MainActivity.java:54) 10-11 03:40:49.260: E/AndroidRuntime(561): at java.util.Timer$TimerImpl.run(Timer.java:284) 
+4
source share
4 answers
 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

This is problem

You need to move the part of the background task that updates ui to the main thread or create mHandler in your user interface thread;

Update:

Timer UI Update: http://android-developers.blogspot.com/2007/11/stitch-in-time.html

+3
source

or use a handler to start updating a text field:

  private final Runnable mRunnable1 = new Runnable() { public void run() { timesince++; editText.setText("Timeran:"+timesince); Log.d("TIMER", "TimerTask run"); mHandler.postDelayed(mRunnable1, 1000); } }; 

and also call this line in the OnCreate() mHandler.postDelayed(mRunnable1, 1000);

+2
source

Try updating the TextView from the user interface stream. Try using a handler. Here you can find a good article. Refresh text with a handler

0
source

follow these steps:

declare the timer as a global variable and call the timer function in the following method:

  public void startThread(){ time.schedule(new TimerTask() { public void run() { timesince++; editText.setText("Timeran:"+timesince); Log.d("TIMER", "TimerTask run"); }}, 0, 1000); } 

and call this function startThread() in onCreate()

0
source

Source: https://habr.com/ru/post/1439062/


All Articles