タイマーが終了した直後にアプリがクラッシュしています...タイムアウト後にアクティビティ lvl_2 に移動するだけです。
5 つのレベル、つまり Game,lvl2_game,lvl3_game,lvl4_game,lvl5_game 8 つのレイアウト lvl_1,lvl_2,lvl_3,lvl_4,lvl_5 ,menu , home,score を持つゲームです。タイムアウト後に Game.java から lvl2_game に移動する必要がありますが、その戻り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);
// Two 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 i = getIntent();
overridePendingTransition(0, 0);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(i);
}
}
}
}