Android Error: Only the original thread that created a view hierarchy can touch its views

androidmultithreadingtext;

Getting an error: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

I really dont have a clue why.

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button generate = (Button) findViewById(R.id.gen);
    final TextView dice1 = (TextView) findViewById(R.id.dice1);
    final TextView dice2 = (TextView) findViewById(R.id.dice2);
    final TextView dice3 = (TextView) findViewById(R.id.dice3);


    generate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {           
            dice1.setText(" ");
            dice2.setText(" ");
            dice3.setText(" ");         
            Thread thread = new Thread()
            {
                @Override
                public void run() {
                    try {
                        while(true) {
                            sleep(2000);
                            setText("lol", "lol", "lol");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            thread.start();             
        }

    });
}
public void setText(String d1, String d2, String d3){
    dice1.setText(d1);
    dice2.setText(d2);
    dice3.setText(d3);  
}

Thanks for helping.

Best Answer

You have this error because you manipulate views in another thread than the UI thread. Your setText method modify the text of 3 TextViews (dice1, dice2, dice3).

You should call this method with runOnUiThread like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button generate = (Button) findViewById(R.id.gen);
    final TextView dice1 = (TextView) findViewById(R.id.dice1);
    final TextView dice2 = (TextView) findViewById(R.id.dice2);
    final TextView dice3 = (TextView) findViewById(R.id.dice3);

    generate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {           
            dice1.setText(" ");
            dice2.setText(" ");
            dice3.setText(" ");         
            Thread thread = new Thread()
            {
                @Override
                public void run() {
                    try {
                        while(true) {
                            sleep(2000);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    setText("lol", "lol", "lol");
                                }
                            });
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            thread.start();             
        }

    });
}
public void setText(String d1, String d2, String d3){
    dice1.setText(d1);
    dice2.setText(d2);
    dice3.setText(d3);  
}
Related Topic