0

Below is my code it is printing 3 or 4 words then stoping app.

public class MessageWindow extends AppCompatActivity {

String messageFinal = "abcd abcd1 abcd2 abcd3";
private TextView msgDisplay;

public void startThread()
{
    ExampleThraed et=new ExampleThraed();
    et.start();
}

class ExampleThraed extends Thread{

    @Override
    public void run(){
        for(String i : messageFinal.split(" "))
        {
            msgDisplay.append(i+" ");
            try{
                Thread.sleep(100);
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message_window);
    String message = " By Jay";
    TextView pwDisplay = (TextView) findViewById(R.id.textView1);
    pwDisplay.setText(message);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
    msgDisplay = (TextView) findViewById(R.id.textView3);
    msgDisplay.setMovementMethod(new ScrollingMovementMethod());
    startThread();
}

}

Getting Below error:

E/AndroidRuntime: FATAL EXCEPTION: Thread-2
    Process: com.example.onlyforyou, PID: 16056
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6855)
        at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1075)
        at android.view.ViewGroup.invalidateChild(ViewGroup.java:5242)
        at android.view.View.invalidateInternal(View.java:13574)
        at android.view.View.invalidate(View.java:13538)
        at android.view.View.invalidate(View.java:13522)
        at android.widget.TextView.updateAfterEdit(TextView.java:8209)
        at android.widget.TextView.handleTextChanged(TextView.java:8234)
        at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10370)
        at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1208)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:578)
        at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:291)
        at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:289)
        at android.widget.TextView.append(TextView.java:4045)
        at android.widget.TextView.append(TextView.java:4032)
        at com.example.onlyforyou.MessageWindow$ExampleThraed.run(MessageWindow.java:46)
Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27

1 Answers1

0

Here you are updating views from worker thread, though views can be updated via main thread

 msgDisplay.append(i+" ");

instead of this

try to post this to ui thread, there update view via main thread. You can keep a handler of main thread and post this change to main thread handler.

akashzincle
  • 1,108
  • 6
  • 15