1

So I am learning on how to develop android applications. I am trying to get this program to flash some letters, one at a time fairly quickly on a textView, but when I try this on my android device and it does not work I get the message "I/Choreographer﹕ Skipped 57 frames! The application may be doing too much work on its main thread." (AttetionalBlinkTrial is a class that has a field called "blinkList" that is an ArrayList of strings)

public void startTask(View view) throws InterruptedException {
    TextView textView = (TextView) findViewById(R.id.display);
    AttentionalBlinkTrial theTrial = new AttentionalBlinkTrial();
    theTrial.generateTargets();
    theTrial.generateBlinkList(5);
    for (int i = 0; i <= 5; i++) {
        textView.setText(theTrial.getBlinkList().get(i));
        Thread.sleep(40);
        textView.setText(");
        Thread.sleep(40);
    }
}
Bev
  • 13
  • 2

2 Answers2

2

Thread.sleep makes UI thread inaccessible. You should use Handler class instead. Sorry I can't provide any codes since I am on mobile but it's quite easy. If i remember right "postDelayed" method is what you need.

public void blink(TextView textView) {
    if (textView.getText() == "Blink!") {
        textView.setText("");
    } else {
        textView.setText("Blink!");
    }
}

public void blinkingTask() throws InterruptedException {
    final Handler handler = new Handler();
    final TextView textView = (TextView) findViewById(R.id.my_text);
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            blink(textView);
        }
    };

    for (int i = 0; i <= 5; i++) {
        handler.postDelayed(runnable, 1000 + (i * 1000)); // 5 delayed actions with 1000 ms interval.
    }
}
starkm
  • 859
  • 1
  • 10
  • 21
  • Handler isn't the only way to implement asyctasks (Asynctask, Loaders...) however it is enough for your situation. – starkm Nov 17 '14 at 03:28
  • Hi thanks for the help! I am not quite sure what data should go in the handler and what should remain in the current method though. I think the set text should remain in the current method because it is in the UI thread but I am not sure what to post to the handler. Thanks. – Bev Nov 17 '14 at 08:06
  • You are not thinking right at the moment. Everyone does this mistake in the beginning no problem though :) Instead of making the WHOLE application "Wait", you should just "Delay" the execution of that function. So the rest of the program can be kept in schedule. I'll add code to my answer in few minutes. – starkm Nov 17 '14 at 11:38
0

take a look at Update UI from Thread. you should do all the operations on seperate thread

AttentionalBlinkTrial theTrial = new AttentionalBlinkTrial();
theTrial.generateTargets();
theTrial.generateBlinkList(5);

and only set text on UI thread.

Community
  • 1
  • 1
Kirtan
  • 1,782
  • 1
  • 13
  • 35