Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use a handler to update my main Activity?
    text
    copied!<p>I'm a beginner in Java/Android and I'm trying to increase my knowledge doing a simple game. You probably have ever heard about Genius.</p> <p>Well, here's my main activity code:</p> <pre><code>package br.genius; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class GeniusActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button b1 = (Button) findViewById(R.id.button1); final Button b2 = (Button) findViewById(R.id.button2); final Button b3 = (Button) findViewById(R.id.button3); final Button b4 = (Button) findViewById(R.id.button4); Button start = (Button) findViewById(R.id.button5); start.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Principal p = new Principal(b1,b2,b3,b4); } }); } public static void vermelho(int i, ArrayList&lt;Button&gt; alb) { alb.get(i).setBackgroundColor(-65536); } } </code></pre> <p>and here is my class extending a Thread:</p> <pre><code>package br.genius; import java.util.ArrayList; import java.util.Random; import android.widget.Button; public class Principal extends Thread implements Runnable{ private ArrayList&lt;Button&gt; alb = new ArrayList&lt;Button&gt;(); private Button b1,b2,b3,b4; private Thread tt; public Principal(Button b1,Button b2,Button b3,Button b4) { this.b1 = b1; this.b2 = b2; this.b3 = b3; this.b4 = b4; tt = new Thread(this); tt.start(); //this.run(); } public void run() { int ve[] = iniciaSequencia(); for (int i = 0; i &lt; 4; i++) { switch (ve[i]) { case 1: alb.add(i, b1); break; case 2: alb.add(i, b2); break; case 3: alb.add(i, b3); break; case 4: alb.add(i, b4); break; } } for (int i = 0; i &lt; 4; i++) { GeniusActivity.vermelho(i,alb); try { tt.wait(1500); } catch(InterruptedException e) { } //alb.get(i).setBackgroundColor(-7829368); } /*rr = new Runnable(){ public void run(){ for (int i = 0; i &lt; 4; i++) { vermelho(i); //atrasador(); //alb.get(i).setBackgroundColor(-7829368); // } } };*/ } public int[] iniciaSequencia() { int v[] = new int[4]; Random r = new Random(); for (int i = 0; i &lt; 4; i++) { v[i] = r.nextInt(4) + 1; } return v; } } </code></pre> <p>I'm getting an error called $CalledFromWrongThreadException : only the original thread that created a view hierarchy can touch its views.</p> <p>Thanks, Leo</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload