Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid and Threading
    text
    copied!<p>Hullo,</p> <p>I have an activity whose layout is made of a single FrameLayout. I have a class that extends View which is then added onto this layout.</p> <p>I want to continually draw to this view, like in a game loop.</p> <p>Problem is I'm new to android and this Threading stuff is making no sense..</p> <p>Can I really not update the layout unless I'm in the main UI thread? How do I go about initialising Threads? :(</p> <p>EDIT::This is what I've come up with, but it doesn't update my frame with anything.</p> <pre><code> public class GameFrame extends View { private GameThread m_gt; public GameFrame(Context context, int width int height){ //do some initialisation stuff } public GameThread getGameThread(){ if(m_gt != null){ return m_gt; }else{ m_gt = new GameThread(this); return m_gt; } } @Override public void onDraw(Canvas canvas){ //Draw loads of stuff } public class GameThread extends AsyncTask&lt;Void, Void, Void&gt; { private GameFrame m_gf; public GameThread(GameFrame gf){ m_gf = gf; } @Override protected void onProgressUpdate(Void... arg0){ System.out.println("Wahoo"); m_gf.invalidate(); } @Override protected Void doInBackground(Void... arg0) { long start, end; while(some bool){ System.out.println("Threadin'"); publishProgress(); //Fiddle with some values } return null; } } } </code></pre> <p>And then to initialise it in main activity:</p> <pre><code>private FrameLayout frame; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { ... frame = (FrameLayout) findViewById(R.id.gameframe); GameFrame gf = new GameFrame(this, width, height); frame.addView(gf); gf.getGameThread().execute(); } </code></pre> <p>Is my understanding of Async correct? Should this work given valid code in onDraw?</p> <p>I should probably say the print outs "wahoo" and "threading" are being printed out numerous times so something must be happening right. More crucially if it has printed "wahoo" then surely it should next redraw my View...but nothing happens :(</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