java - FATAL exception Thread -12 -
app crashing after timer finishes time... have move activity lvl_2 after timeout .
its game 5 levels namely game,lvl2_game,lvl3_game,lvl4_game,lvl5_game 8 layout lvl_1,lvl_2,lvl_3,lvl_4,lvl_5 ,menu , home,score have move game.java lvl2_game after timeout returning fatal exception : thread -12 java.lang.illigalstateexception
public class game extends activity implements sensoreventlistener{ mediaplayer coinsong; float fx1,fx2,fx3,fx4,fy1,fy2,fy3,fy4; float xb1,xb2,xb3,xb4,yb1,yb2,yb3,yb4; int result=0; float xc1_1,xc1_2,xc1_3,xc1_4,yc1_1,yc1_2,yc1_3,yc1_4; int chk=1,delay,chkc1=0,chkc2=0,chkc3=0,chkc4=0,chkc5=0,chkc6=0,chkc7=0,chkc8=0,chkc9=0,chkc10=0,chkc11=0,chkc12=0; float xc2_1,xc2_2,xc2_3,xc2_4,yc2_1,yc2_2,yc2_3,yc2_4; float xc3_1,xc3_2,xc3_3,xc3_4,yc3_1,yc3_2,yc3_3,yc3_4; float xc4_1,xc4_2,xc4_3,xc4_4,yc4_1,yc4_2,yc4_3,yc4_4; float xc5_1,xc5_2,xc5_3,xc5_4,yc5_1,yc5_2,yc5_3,yc5_4; float xc6_1,xc6_2,xc6_3,xc6_4,yc6_1,yc6_2,yc6_3,yc6_4; float xc7_1,xc7_2,xc7_3,xc7_4,yc7_1,yc7_2,yc7_3,yc7_4; float xc8_1,xc8_2,xc8_3,xc8_4,yc8_1,yc8_2,yc8_3,yc8_4; float xc9_1,xc9_2,xc9_3,xc9_4,yc9_1,yc9_2,yc9_3,yc9_4; float xc10_1,xc10_2,xc10_3,xc10_4,yc10_1,yc10_2,yc10_3,yc10_4; float xc11_1,xc11_2,xc11_3,xc11_4,yc11_1,yc11_2,yc11_3,yc11_4; float xc12_1,xc12_2,xc12_3,xc12_4,yc12_1,yc12_2,yc12_3,yc12_4; float xe1,xe2,xe3,xe4,xe11,xe22,xe33,xe44,ye1,ye2,ye3,ye4,ye11,ye22,ye33,ye44; float sensorx, sencurrx, x, y, eatx=0, eaty, ballx, bally, sencurry, sensory; float eatx2=eatx, eaty2=eaty+100; int score; bitmap ball, finish, eat, eat1, eat2, eat3, eat4, coin, coin1, coin2, coin3, coin4, coin5, coin6, coin7, coin8, coin9, coin10, coin11, wallp; sensormanager sm; wakelock wl; mysurface game_surface ; public class mysurface extends surfaceview implements runnable { surfaceholder my_holder; thread game_thread= null; boolean isrunning= false; public mysurface(context context){ // todo auto-generated constructor stub super(context); my_holder = getholder(); } public void pause(){ isrunning =false; while(true){ try { game_thread.join(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } break; } game_thread= null; } public void resume(){ isrunning=true; game_thread = new thread(this); game_thread.start(); wl.acquire(); } @override public void run() { // todo auto-generated method stub while(isrunning){ if(!my_holder.getsurface().isvalid()) continue; canvas canvas= my_holder.lockcanvas(); //delay++; canvas.drawrgb(175,225,251); //canvas.drawbitmap(wallp,0, 0, null); float centerx=(canvas.getwidth()/2)-(ball.getwidth()/2); float centery=(canvas.getheight()/2)-(ball.getheight()/2); canvas.drawbitmap(finish,0,350, null); // coin layer 1 if(chkc1==0) canvas.drawbitmap(coin,50,50,null); if(chkc2==0) canvas.drawbitmap(coin1,50+coin.getwidth(),50,null); if(chkc3==0) canvas.drawbitmap(coin2,50+coin.getwidth()*2,50,null); if(chkc4==0) canvas.drawbitmap(coin3,50+coin.getwidth()*3,50,null); // coin layer 2 if(chkc5==0) canvas.drawbitmap(coin4,100,150,null); if(chkc6==0) canvas.drawbitmap(coin5,100+coin.getwidth(),150,null); if(chkc7==0) canvas.drawbitmap(coin6,100+coin.getwidth()*2,150,null); if(chkc8==0) canvas.drawbitmap(coin7,100+coin.getwidth()*3,150,null); // coin layer 3 if(chkc9==0) canvas.drawbitmap(coin8,50,250,null); if(chkc10==0) canvas.drawbitmap(coin9,50+coin.getwidth(),250,null); if(chkc11==0) canvas.drawbitmap(coin10,50+coin.getwidth()*2,250,null); if(chkc12==0) canvas.drawbitmap(coin11,50+coin.getwidth()*3,250,null); // 2 eat canvas.drawbitmap(eat1, eatx,eaty, null); float eatt; eatt=(canvas.getheight()- finish.getheight())- eat.getheight()-eaty2; canvas.drawbitmap(eat, eatx2,eatt , null); ballx=(centerx+sensorx*20); decimalformat df = new decimalformat("#.#"); ballx=float.valueof(df.format(ballx)); decimalformat df2 = new decimalformat("#.#"); bally=(centery+sensory*50); bally=float.valueof(df2.format(bally)); // coordinates of coins xc1_1=50; xc1_2=50+coin.getwidth(); xc1_3=50+coin.getwidth(); xc1_4=50; yc1_1=50; yc1_2=50; yc1_3=50-coin.getheight(); yc1_4=50-coin.getheight(); xc2_1=50+coin.getwidth(); xc2_2=50+coin.getwidth()*2; xc2_3=50+coin.getwidth()*2; xc2_4=50+coin.getwidth(); yc2_1=50; yc2_2=50; yc2_3=50-coin.getheight(); yc2_4=50-coin.getheight(); xc3_1=50+coin.getwidth()*2; xc3_2=50+coin.getwidth()*3; xc3_3=50+coin.getwidth()*3; xc3_4=50+coin.getwidth()*2; yc3_1=50; yc3_2=50; yc3_3=50-coin.getheight(); yc3_4=50-coin.getheight(); xc4_1=50+coin.getwidth()*3; xc4_2=50+coin.getwidth()*4; xc4_3=50+coin.getwidth()*4; xc4_4=50+coin.getwidth()*3; yc4_1=50; yc4_2=50; yc4_3=50-coin.getheight(); yc4_4=50-coin.getheight(); xc5_1=100; xc5_2=100+coin.getwidth(); xc5_3=100+coin.getwidth(); xc5_4=100; yc5_1=150; yc5_2=150; yc5_3=150-coin.getheight(); yc5_4=150-coin.getwidth(); xc6_1=100+coin.getwidth(); xc6_2=100+coin.getwidth()*2; xc6_3=100+coin.getwidth()*2; xc6_4=100+coin.getwidth(); yc6_1=150; yc6_2=150; yc6_3=150-coin.getheight(); yc6_4=150-coin.getheight(); xc7_1=100+coin.getwidth()*2; xc7_2=100+coin.getwidth()*3; xc7_3=100+coin.getwidth()*3; xc7_4=100+coin.getwidth()*2; yc7_1=150; yc7_2=150; yc7_3=150-coin.getheight(); yc7_4=150-coin.getheight(); xc8_1=100+coin.getwidth()*3; xc8_2=100+coin.getwidth()*4; xc8_3=100+coin.getwidth()*4; xc8_4=100+coin.getwidth()*3; yc8_1=150; yc8_2=150; yc8_3=150-coin.getheight(); yc8_4=150-coin.getheight(); xc9_1=50; xc9_2=50+coin.getwidth(); xc9_3=50+coin.getwidth(); xc9_4=50; yc9_1=250; yc9_2=250; yc9_3=250-coin.getheight(); yc9_4=250-coin.getheight(); xc10_1=50+coin.getwidth(); xc10_2=50+coin.getwidth()*2; xc10_3=50+coin.getwidth()*2; xc10_4=50+coin.getwidth(); yc10_1=250; yc10_2=250; yc10_3=250-coin.getheight(); yc10_4=250-coin.getwidth(); xc11_1=50+coin.getwidth()*2; xc11_2=50+coin.getwidth()*3; xc11_3=50+coin.getwidth()*3; xc11_4=50+coin.getwidth()*2; yc11_1=250; yc11_2=250; yc11_3=250-coin.getheight(); yc11_4=250-coin.getwidth(); xc12_1=50+coin.getwidth()*3; xc12_2=50+coin.getwidth()*4; xc12_3=50+coin.getwidth()*4; xc12_4=50+coin.getwidth()*3; yc12_1=250; yc12_2=250; yc12_3=250-coin.getheight(); yc12_4=250-coin.getwidth(); // ball touching coin if( ((xb1>xc1_4)&&(xb1<xc1_2))&&((yb1>yc1_4)&&(yb1<yc1_2))|| ((xb2>xc1_4)&&(xb2<xc1_2))&&((yb2>yc1_4)&&(yb2<yc1_2)) || ((xb3>xc1_4)&&(xb3<xc1_2))&&((yb3>yc1_4)&&(yb3<yc1_2)) || ((xb4>xc1_4)&&(xb4<xc1_2))&&((yb4>yc1_4)&&(yb4<yc1_2)) ) { chkc1=1; result+=5; coinsong.start(); } if( ((xb1>xc2_4)&&(xb1<xc2_2))&&((yb1>yc2_4)&&(yb1<yc2_2))|| ((xb2>xc2_4)&&(xb2<xc2_2))&&((yb2>yc2_4)&&(yb2<yc2_2)) || ((xb3>xc2_4)&&(xb3<xc2_2))&&((yb3>yc2_4)&&(yb3<yc2_2)) || ((xb4>xc2_4)&&(xb4<xc2_2))&&((yb4>yc2_4)&&(yb4<yc2_2)) ) { chkc2=1; coinsong.start(); result+=5; } if( ((xb1>xc3_4)&&(xb1<xc3_2))&&((yb1>yc3_4)&&(yb1<yc3_2))|| ((xb2>xc3_4)&&(xb2<xc3_2))&&((yb2>yc3_4)&&(yb2<yc3_2)) || ((xb3>xc3_4)&&(xb3<xc3_2))&&((yb3>yc3_4)&&(yb3<yc3_2)) || ((xb4>xc3_4)&&(xb4<xc3_2))&&((yb4>yc3_4)&&(yb4<yc3_2)) ) { chkc3=1; result+=5; coinsong.start(); } if( ((xb1>xc4_4)&&(xb1<xc4_2))&&((yb1>yc4_4)&&(yb1<yc4_2))|| ((xb2>xc4_4)&&(xb2<xc4_2))&&((yb2>yc4_4)&&(yb2<yc4_2)) || ((xb3>xc4_4)&&(xb3<xc4_2))&&((yb3>yc4_4)&&(yb3<yc4_2)) || ((xb4>xc4_4)&&(xb4<xc4_2))&&((yb4>yc4_4)&&(yb4<yc4_2)) ) { chkc4=1; coinsong.start(); result+=5; } if( ((xb1>xc5_4)&&(xb1<xc5_2))&&((yb1>yc5_4)&&(yb1<yc5_2))|| ((xb2>xc5_4)&&(xb2<xc5_2))&&((yb2>yc5_4)&&(yb2<yc5_2)) || ((xb3>xc5_4)&&(xb3<xc5_2))&&((yb3>yc5_4)&&(yb3<yc5_2)) || ((xb4>xc5_4)&&(xb4<xc5_2))&&((yb4>yc5_4)&&(yb4<yc5_2)) ) { result+=5; chkc5=1; coinsong.start(); } if( ((xb1>xc6_4)&&(xb1<xc6_2))&&((yb1>yc6_4)&&(yb1<yc6_2))|| ((xb2>xc6_4)&&(xb2<xc6_2))&&((yb2>yc6_4)&&(yb2<yc6_2)) || ((xb3>xc6_4)&&(xb3<xc6_2))&&((yb3>yc6_4)&&(yb3<yc6_2)) || ((xb4>xc6_4)&&(xb4<xc6_2))&&((yb4>yc6_4)&&(yb4<yc6_2)) ) { result+=5; chkc6=1; coinsong.start(); } if( ((xb1>xc7_4)&&(xb1<xc7_2))&&((yb1>yc7_4)&&(yb1<yc7_2))|| ((xb2>xc7_4)&&(xb2<xc7_2))&&((yb2>yc7_4)&&(yb2<yc7_2)) || ((xb3>xc7_4)&&(xb3<xc7_2))&&((yb3>yc7_4)&&(yb3<yc7_2)) || ((xb4>xc7_4)&&(xb4<xc7_2))&&((yb4>yc7_4)&&(yb4<yc7_2)) ) { result+=5; chkc7=1; coinsong.start(); } if( ((xb1>xc8_4)&&(xb1<xc8_2))&&((yb1>yc8_4)&&(yb1<yc8_2))|| ((xb2>xc8_4)&&(xb2<xc8_2))&&((yb2>yc8_4)&&(yb2<yc8_2)) || ((xb3>xc8_4)&&(xb3<xc8_2))&&((yb3>yc8_4)&&(yb3<yc8_2)) || ((xb4>xc8_4)&&(xb4<xc8_2))&&((yb4>yc8_4)&&(yb4<yc8_2)) ) { result+=5; coinsong.start(); chkc8=1; } if( ((xb1>xc9_4)&&(xb1<xc9_2))&&((yb1>yc9_4)&&(yb1<yc9_2))|| ((xb2>xc9_4)&&(xb2<xc9_2))&&((yb2>yc9_4)&&(yb2<yc9_2)) || ((xb3>xc9_4)&&(xb3<xc9_2))&&((yb3>yc9_4)&&(yb3<yc9_2)) || ((xb4>xc9_4)&&(xb4<xc9_2))&&((yb4>yc9_4)&&(yb4<yc9_2)) ) { result+=5; coinsong.start(); chkc9=1; } if( ((xb1>xc10_4)&&(xb1<xc10_2))&&((yb1>yc10_4)&&(yb1<yc10_2))|| ((xb2>xc10_4)&&(xb2<xc10_2))&&((yb2>yc10_4)&&(yb2<yc10_2)) || ((xb3>xc10_4)&&(xb3<xc10_2))&&((yb3>yc10_4)&&(yb3<yc10_2)) || ((xb4>xc10_4)&&(xb4<xc10_2))&&((yb4>yc10_4)&&(yb4<yc10_2)) ) { result+=5; coinsong.start(); chkc10=1; } if( ((xb1>xc11_4)&&(xb1<xc11_2))&&((yb1>yc11_4)&&(yb1<yc11_2))|| ((xb2>xc11_4)&&(xb2<xc11_2))&&((yb2>yc11_4)&&(yb2<yc11_2)) || ((xb3>xc11_4)&&(xb3<xc11_2))&&((yb3>yc11_4)&&(yb3<yc11_2)) || ((xb4>xc11_4)&&(xb4<xc11_2))&&((yb4>yc11_4)&&(yb4<yc11_2)) ) { result+=5; coinsong.start(); chkc11=1; } if( ((xb1>xc12_4)&&(xb1<xc12_2))&&((yb1>yc12_4)&&(yb1<yc12_2))|| ((xb2>xc12_4)&&(xb2<xc12_2))&&((yb2>yc12_4)&&(yb2<yc12_2)) || ((xb3>xc12_4)&&(xb3<xc12_2))&&((yb3>yc12_4)&&(yb3<yc12_2)) || ((xb4>xc12_4)&&(xb4<xc12_2))&&((yb4>yc12_4)&&(yb4<yc12_2)) ) { result+=5; coinsong.start(); chkc12=1; } // ball coordinates xb1=ballx; xb2=ballx+ball.getwidth(); xb3=ballx; xb4=ballx+ball.getwidth(); yb1=bally; yb2=bally; yb3=bally-ball.getheight(); yb4=bally-ball.getwidth(); //eat coordinates xe1=eatx; xe2=eatx+eat.getwidth(); xe3=eatx+eat.getwidth(); xe4=eatx; ye1=eaty; ye2=eaty; ye3=eaty-eat.getheight(); ye4=eaty-eat.getheight(); xe11=eatx2; xe22=eatx2+eat.getwidth(); xe33=eatx2+eat.getwidth(); xe44=eatx2; ye11=eatt; ye22=eatt; ye33=eatt-eat.getheight(); ye44=eatt-eat.getheight(); // ball touching eat if( ((xb1>xe4)&&(xb1<xe2))&&((yb1>ye4)&&(yb1<ye2))|| ((xb2>xe4)&&(xb2<xe2))&&((yb2>ye4)&&(yb2<ye2)) || ((xb3>xe4)&&(xb3<xe2))&&((yb3>ye4)&&(yb3<ye2)) || ((xb4>xe4)&&(xb4<xe2))&&((yb4>ye4)&&(yb4<ye2)) ) { intent menu = new intent(game.this,score.class); menu.addflags(intent.flag_activity_clear_top); startactivity(menu); finish(); } else canvas.drawbitmap(ball, ballx,bally, null); if( ((xb1>xe44)&&(xb1<xe22))&&((yb1>ye44)&&(yb1<ye22))|| ((xb2>xe44)&&(xb2<xe22))&&((yb2>ye44)&&(yb2<ye22)) || ((xb3>xe44)&&(xb3<xe22))&&((yb3>ye44)&&(yb3<ye22)) || ((xb4>xe44)&&(xb4<xe22))&&((yb4>ye44)&&(yb4<ye22)) ) { intent menu = new intent(game.this,score.class); menu.addflags(intent.flag_activity_clear_top); startactivity(menu); finish(); } else canvas.drawbitmap(ball, ballx,bally, null); // finish coordinate fx1=0; fx2=5; fx3=0+finish.getwidth(); fx4=0; fy1=350; fy2=350; fy3=350+finish.getheight(); fy4=400; // touching finish if( ((xb1>fx4)&&(xb1<fx2))&&((yb1>fy4)&&(yb1<fy2))|| ((xb2>fx4)&&(xb2<fx2))&&((yb2>fy4)&&(yb2<fy2)) || ((xb3>fx4)&&(xb3<fx2))&&((yb3>fy4)&&(yb3<fy2)) || ((xb4>fx4)&&(xb4<fx2))&&((yb4>fy4)&&(yb4<fy2)) ) { intent nextscreen = new intent(game.this,lvl_2.class); nextscreen.addflags(intent.flag_activity_clear_top); startactivity(nextscreen); finish(); } else canvas.drawbitmap(ball, ballx,bally, null); eaty=eaty+2; eaty2=eaty-2; eatx=eatx+1; eatx2=eatx2-1; if(eaty>canvas.getheight()){ eaty=0; } if(eatx>canvas.getwidth()){ eatx=0; } if(eatx2<0){ eatx2=canvas.getwidth(); } if(eaty2<0){ eaty2=canvas.getwidth(); } my_holder.unlockcanvasandpost(canvas); } } } @override public void onbackpressed() { // todo auto-generated method stub super.onbackpressed(); intent menu = new intent(game.this,menu.class); menu.addflags(intent.flag_activity_clear_top); startactivity(menu); } @suppresswarnings("deprecation") @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); // full screen requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); //wake-lock powermanager pm = (powermanager) getsystemservice(context.power_service); wl = pm.newwakelock(powermanager.full_wake_lock, "whatever"); wl.acquire(); sm = (sensormanager) getsystemservice(context.sensor_service); if (sm.getsensorlist(sensor.type_accelerometer).size()!= 0){ sensor s= sm.getsensorlist(sensor.type_accelerometer).get(0) ; sm.registerlistener(this , s,sensormanager.sensor_delay_game); } finish=bitmapfactory.decoderesource(getresources(), r.drawable.home); ball = bitmapfactory.decoderesource(getresources(), r.drawable.ball); //wallp = bitmapfactory.decoderesource(getresources(), r.drawable.lvl1_bck); eat = bitmapfactory.decoderesource(getresources(),r.drawable.rev); eat1 = bitmapfactory.decoderesource(getresources(),r.drawable.ur2l5); coin= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin1= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin2= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin3= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin4= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin5= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin6= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin7= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin8= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin9= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin10= bitmapfactory.decoderesource(getresources(),r.drawable.coins); coin11= bitmapfactory.decoderesource(getresources(),r.drawable.coins); x=y=sensorx=sensory=0; eatx=0; eaty=5; //x=y=sensorx=sensory=0; game_surface = new mysurface(this); game_surface.resume(); setcontentview(game_surface); timertask task = new timertask(){public void run() { intent it=new intent(game.this,lvl_2.class); startactivity(it); } }; timer t=new timer(); t.schedule(task, 5*1000); coinsong = mediaplayer.create(game.this,r.raw.coins); } @override public void onaccuracychanged(sensor arg0, int arg1) { // todo auto-generated method stub } @override public void onsensorchanged(sensorevent e) { // todo auto-generated method stub if(chk==1) { sencurrx=e.values[0]; sencurry=e.values[1]; chk=2; } try { thread.sleep(5); } catch (interruptedexception e1) { // todo auto-generated catch block e1.printstacktrace(); } sensorx = e.values[0]-sencurrx; sensory = e.values[1]-sencurry; } @override protected void onpause() { // todo auto-generated method stub super.onpause(); coinsong.release(); sm.unregisterlistener(this); wl.release(); finish(); } public void onactivityresult(int requestcode, int resultcode, intent intent) { if (requestcode == 1) { if (resultcode == 1) { intent = getintent(); overridependingtransition(0, 0); i.addflags(intent.flag_activity_no_animation); finish(); overridependingtransition(0, 0); startactivity(i); } } } }
i think crash caused code:
intent menu = new intent(game.this,score.class); menu.addflags(intent.flag_activity_clear_top); startactivity(menu); finish();
why did put startactivity in thread not main thread? should use handler send message main thread start new activity.
Comments
Post a Comment