我正在制作幻灯片益智游戏,但不知道异常发生在哪里并且应用程序崩溃。我使用 Logcat,发现多次调用 onTouch() 后它会崩溃。以下是代码
public boolean onTouch(View v,MotionEvent event){
float temp;
Log.d("Puz","onTouch1 ");
x= event.getX();
y= event.getY();
fl1=0;
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
float x1=max_x/no_b,y1=max_y/no_b;
cur_x=x-x%x1;
cur_y=y-y%y1;
x_p=(int)cur_x/(int)x1;
y_p=(int)cur_y/(int)y1;
cur_x=x_p*(int)x1;
cur_y=y_p*(int)y1;
b_i=cur_x;
b_j=cur_y;
if(x_p+1<no_b&&cha_c[x_p+1][y_p]==-1){
x_m_f=b_i;
x_m_t=b_i+2*x_di;
y_m_f=b_j;
y_m_t=b_j+y_di;
set=2;
}
else if(x_p-1>-1&&cha_c[x_p-1][y_p]==-1){
x_m_f=b_i-x_di;
x_m_t=b_i+x_di;
y_m_f=b_j;
y_m_t=b_j+y_di;
set=0;
}
else if(y_p+1<no_b&&cha_c[x_p][y_p+1]==-1){
x_m_f=b_i;
x_m_t=b_i+x_di;
y_m_f=b_j;
y_m_t=b_j+2*y_di;
set=3;
}
else if(y_p-1>-1&&cha_c[x_p][y_p-1]==-1){
x_m_f=b_i;
x_m_t=b_i+x_di;
y_m_f=b_j-y_di;
y_m_t=b_j+y_di;
set=1;
}
else
x_p=-1;
pre_x=x;
pre_y=y;
break;
case MotionEvent.ACTION_UP:
int temp_x,temp_y;
if(x_p!=-1) {
if (set == 0 && x_m_f + x_di > cur_x + x_di / 2) {
cha_c_p[x_p - 1][y_p] = cha_c_p[x_p][y_p];
cha_c_p[x_p][y_p] = null;
cha_c[x_p - 1][y_p] = cha_c[x_p][y_p];
cha_c[x_p][y_p] = -1;
////temp_x=(cha_c[x_p - 1][y_p]-1)%no_b;
// temp_y=(cha_c[x_p - 1][y_p]-1)/no_b;
//cha_c_p[x_p - 1][y_p] = Bitmap.createBitmap(test, temp_x * (int)x_di, temp_y * (int)y_di, (int)x_di-2, (int)y_di-2);
} else if (set == 2 && x_m_f + x_di < cur_x + x_di / 2) {
cha_c_p[x_p + 1][y_p] = cha_c_p[x_p][y_p];
cha_c_p[x_p][y_p] = null;
cha_c[x_p + 1][y_p] = cha_c[x_p][y_p];
cha_c[x_p][y_p] = -1;
// temp_x=(cha_c[x_p + 1][y_p]-1)%no_b;
//temp_y=(cha_c[x_p + 1][y_p]-1)/no_b;
//cha_c_p[x_p + 1][y_p] = Bitmap.createBitmap(test, temp_x * (int)x_di, temp_y * (int)y_di, (int)x_di-2, (int)y_di-2);
} else if (set == 1 && y_m_f + y_di > cur_y + y_di / 2) {
cha_c_p[x_p ][y_p- 1] = cha_c_p[x_p][y_p];
cha_c_p[x_p][y_p] = null;
cha_c[x_p][y_p - 1] = cha_c[x_p][y_p];
cha_c[x_p][y_p] = -1;
// temp_x=(cha_c[x_p][y_p- 1]-1)%no_b;
//temp_y=(cha_c[x_p][y_p- 1]-1)/no_b;
//cha_c_p[x_p][y_p-1] = Bitmap.createBitmap(test, temp_x * (int)x_di, temp_y * (int)y_di, (int)x_di-2, (int)y_di-2);
} else if (set == 3 && y_m_f + y_di < cur_y + y_di / 2) {
cha_c_p[x_p ][y_p + 1] = cha_c_p[x_p][y_p];
cha_c_p[x_p][y_p] = null;
cha_c[x_p][y_p + 1] = cha_c[x_p][y_p];
cha_c[x_p][y_p] = -1;
//temp_x=(cha_c[x_p][y_p+ 1]-1)%no_b;
//temp_y=(cha_c[x_p][y_p+ 1]-1)/no_b;
//cha_c_p[x_p ][y_p+1] = Bitmap.createBitmap(test, temp_x * (int)x_di, temp_y * (int)y_di, (int)x_di-2, (int)y_di-2);
}
}
x_p = -1;
break;
}
if(x_p!=-1&&pre_x!=x&&pre_y!=y) {
temp = cur_x + x - pre_x;
if (temp >= x_m_f && temp + x_di <= x_m_t)
cur_x = temp;
temp = cur_y + y - pre_y;
if (temp >= y_m_f && temp + y_di <= y_m_t)
cur_y = temp;
}
pre_x=x;
pre_y=y;
Log.d("Puz","onTouch2 ");
return true;
}
以下是我在 Logcat 中得到的结果
03-07 22:08:04.746 19758-19785/com.sudoku.mohammed.slidepuzzlesolver D/Puz﹕ ye run() 2
03-07 22:08:04.756 19758-19785/com.sudoku.mohammed.slidepuzzlesolver D/Puz﹕ ye touch ke bad
03-07 22:08:04.756 19758-19758/com.sudoku.mohammed.slidepuzzlesolver D/Puz﹕ onTouch1
03-07 22:08:04.756 19758-19758/com.sudoku.mohammed.slidepuzzlesolver D/Puz﹕ onTouch2
03-07 22:08:04.766 19758-19758/com.sudoku.mohammed.slidepuzzlesolver D/Puz﹕ onTouch1
03-07 22:08:04.766 19758-19758/com.sudoku.mohammed.slidepuzzlesolver D/Puz﹕ onTouch2
03-07 22:08:04.766 19758-19758/com.sudoku.mohammed.slidepuzzlesolver D/Puz﹕ onTouch1
03-07 22:08:04.766 19758-19758/com.sudoku.mohammed.slidepuzzlesolver D/Puz﹕ onTouch2
03-07 22:08:04.766 19758-19785/com.sudoku.mohammed.slidepuzzlesolver W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x42016898)
03-07 22:08:04.776 19758-19785/com.sudoku.mohammed.slidepuzzlesolver E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-14604
java.lang.ArrayIndexOutOfBoundsException: length=4; index=-1
at com.sudoku.mohammed.slidepuzzlesolver.SlideSolver$MyBringBackSurface.run(SlideSolver.java:340)
at java.lang.Thread.run(Thread.java:841)