I have written a game (I'll give it the name "Gamer") in which I have the following general structure (the code has been much simplified):
public class Gamer extends Activity implements View.OnClickListener
{
public void onCreate(Bundle savedInstanceState)
{
mpt = new MicksPanelThing(this);
}
}
public class MicksPanelThing extends SurfaceView implements SurfaceHolder.Callback
{
public MicksPanelThing(Context context)
{
super(context);
micks_thread_thing = new MicksThreadThing(getHolder(), this);
getHolder().addCallback(this);
setFocusable(true);
}
void updateView()
{
final SurfaceHolder holder = getHolder();
try
{
Canvas canvas = holder.lockCanvas();
if(canvas != null)
{
onDraw(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void onDraw(Canvas canvas)
{
// blah
}
}
class MicksThreadThing extends Thread
{
public MicksThreadThing(SurfaceHolder surfaceHolder, MicksPanelThing mpt)
{
// blah
}
public void run()
{
while (this_thread_is_currently_active)
{
micks_panel_thing.updateView();
}
}
}
The game was all working fine and was very robust. Now I wanted to test for a certain condition and, if it was true, put up an AlertDialog. I presumed that I could put this code within my onDraw method. I then got a message "Can't create handler inside thread that has not called Looper.prepare()" - this is confusing me because I assumed that the onDraw method was being executed by the main thread for which I presumed I needn't set anything up.
I presume I could solve this by either moving my test and dialog to somewhere within the main thread (where?) or by adding some Looper.prepare() code somewhere.
Please can someone tell me which would be easier - and where should any necessary code go.