android - UIThread is causing problems when I want to open a new Activity -
i have surfaceview in activity, , want open new activity when occurs in surfaceview(when run out of lives - lives == 0). i've tried different things, keep having problems it. if stop uithread first, of course won't keep running , won't able execute startactivity statement. if start activity, freezes on me , force closes - having uithread believe. has run problem before - , if so, have idea how might go achieving this. @ least, if can't open new activity, how close current activity (from inside surfaceview).
public class boardview extends surfaceview implements surfaceholder.callback{ context mcontext; // thread initialization private boardthread thread; thread timer; thread timer2; // box variables bitmap box = (bitmapfactory.decoderesource (getresources(), r.drawable.box)); private int box_x = 140; private int box_y = 378; private int boxwidth = box.getwidth(); private int boxheight = box.getheight(); // storage private vector<blossom> blossomvector = new vector<blossom>(); iterator<blossom> dataiterator = blossomvector.iterator(); // counters private int blossomnum = 0; private string score; private int currentscore = 0; private int lives = 3; boolean mode = false; boolean game = false; outputstreamwriter out = null; fileoutputstream fout = null; private static final string tag = "debug"; final paint scorepaint = new paint(); public boardview(context context){ super(context); scorepaint.setcolor(color.black); scorepaint.settextsize(12); scorepaint.settypeface(typeface.monospace); //surfaceholder provides canvas draw on getholder().addcallback(this); //set read/write data file root = environment.getexternalstoragedirectory(); file highscoresfile = new file(root, "highscores.txt"); // controls drawings thread = new boardthread(getholder(),this, blossomvector, dataiterator, box_x, box_y, boxwidth, boxheight); timer2 = new thread(){ public void run(){ while(game == false){ uicallback.sendemptymessage(0); try{ thread.sleep(5000); // change random } catch (interruptedexception e){ e.printstacktrace(); } } } }; timer = new thread(){ public void run(){ //makes sure player still has 3 lives left while(game == false){ uicallback.sendemptymessage(0); try { thread.sleep(2000); // wait 2 seconds before drawing next flower } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } //sleep 2 seconds } } }; timer.start(); timer2.start(); //intercepts touch events setfocusable(true); } @override public void ondraw(canvas canvas){ canvas.drawcolor(color.white); score = "score: " + currentscore; //note: pay attention order draw things //don't change order or else blossoms fall //on top of box, not "into" it. //display scoreboard canvas.drawtext(score,240,420,scorepaint); // uses synchronized method prevent concurrent modification drawblossoms(canvas); canvas.drawbitmap(box, box_x, box_y, null); } @override public boolean ontouchevent(motionevent event){ //handles movement of box if(event.getaction() == motionevent.action_down){ if(event.getx() > box_x & event.gety() > box_y & event.getx() < box_x + boxwidth & event.gety() < box_y + boxheight) { mode = true; } } if(event.getaction() == motionevent.action_move) { if(event.getx() > box_x & event.gety() > box_y & event.getx() < box_x + boxwidth & event.gety() < box_y + boxheight) { mode = true; } if(mode == true){ box_x = (int)event.getx(); } } if(event.getaction() == motionevent.action_up){ mode = false; } invalidate(); return true; } @override public void surfacechanged(surfaceholder holder, int format, int width, int height ){ log.v(tag, "surface changed"); //somehow these don't seem working } @override public void surfacecreated(surfaceholder holder){ thread.startrunning(true); thread.start(); } @override public void surfacedestroyed(surfaceholder holder){ log.v(tag, "surface destroyed"); try { thread.join(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } private handler uicallback = new handler(){ public synchronized void handlemessage(message msg){ //add new blossom blossom vector!! blossomvector.add(new blossom( (bitmapfactory.decoderesource (getresources(), r.drawable.blossom)))); dataiterator = blossomvector.iterator(); blossomnum++; log.v(tag, "number of blossoms =" + blossomnum); } }; private synchronized void drawblossoms(canvas c) // method draw flowers on screen , test collision { canvas canvas = c; dataiterator = blossomvector.iterator(); while (dataiterator.hasnext()) { blossom tempblossom = dataiterator.next(); tempblossom.draw(canvas); if (tempblossom.hit(box_x,box_y, box_x + boxwidth, box_y + boxheight, blossomvector) == true) { log.v(tag, "iterator works!"); dataiterator.remove(); currentscore += 100; } if (tempblossom.dropped() == true) { dataiterator.remove(); log.v(tag, "blossom dropped"); lives--; } if (lives == 0) { // stop thread makes blossoms game = true; //save highscore try { file root = environment.getexternalstoragedirectory(); if(root.canwrite()){ file highscoresfile = new file(root, "highscores.txt"); filewriter writer = new filewriter(highscoresfile); bufferedwriter out = new bufferedwriter(writer); //out.newline(); out.write(score); out.close(); } } catch (filenotfoundexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { file root = environment.getexternalstoragedirectory(); file highscoresfile = new file(root, "highscores.txt"); filereader reader = new filereader(highscoresfile); bufferedreader in = new bufferedreader(reader); try { string scoretest = in.readline(); log.v(tag, "score: " + scoretest); reader.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (filenotfoundexception e1) { // todo auto-generated catch block e1.printstacktrace(); } try { thread.join(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }
}
board thread`public class boardthread extends thread {
private surfaceholder surfaceholder; private boardview boardview; private vector<blossom> blossomvector; private int boxx; private int boxy; private int boxwidth; private int boxheight; private boolean mrun =false; private iterator<blossom> iterator; private static final string tag = "debug"; public boardthread(surfaceholder holder, boardview boardview2, vector<blossom> blossomvector1, iterator<blossom> dataiterator, int box_x, int box_y, int boxw, int boxh) { surfaceholder = holder; boardview=boardview2; blossomvector = blossomvector1; iterator = dataiterator; boxx = box_x; boxy = box_y; boxw = boxwidth; boxh = boxheight; } public void startrunning(boolean run) { mrun=run; } @override public void run() { super.run(); canvas canvas; while (mrun) { canvas=null; try { canvas = surfaceholder.lockcanvas(null); synchronized (surfaceholder) { //update position //position(blossomvector, boxx, boxy, boxwidth, boxheight); // draw flowers boardview.ondraw(canvas); } } { if (canvas != null) { surfaceholder.unlockcanvasandpost(canvas); } } } } private synchronized void position(vector<blossom> blossomvector,int box_x, int box_y, int boxwidth, int boxheight) { //for(blossom blossom: blossomvector) iterator = blossomvector.iterator(); while (iterator.hasnext()) { blossom tempblossom = iterator.next(); tempblossom.updateposition(); } } }
`
sounds surfaceview running in different thread main ui thread , can't modify view objects. you'll want create handler in activity , make accessible surfaceview. can send message handler , can update ui (or launch new activity) in main ui thread.
Comments
Post a Comment