这是完整的 java 类(GFXSurface)。在这个类中,定义了第二个类,
public class GFXSurface extends Activity implements OnTouchListener {
AnotherSurface ourSurfaceView;
float x, y;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ourSurfaceView = new AnotherSurface(this);
ourSurfaceView.setOnTouchListener(this);
x = 0;
y = 0;
setContentView(ourSurfaceView);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSurfaceView.ourPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
ourSurfaceView.ourResume();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
return true;
}
/*------------------------Class within a class-------------------------------*/
public class AnotherSurface extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning;
public AnotherSurface(Context context) {
// TODO Auto-generated constructor stub
super(context); //not auto-generated; set it up manually
isRunning = false;
ourHolder = getHolder();
}
public void ourPause(){
isRunning = false;
while(true){
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void ourResume(){
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
canvas.drawRGB(255, 0, 0);
if(x!=0 && y!=0){
Bitmap ourBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green_ball);
canvas.drawBitmap(ourBitmap, x-(ourBitmap.getWidth()/2), y-(ourBitmap.getHeight()/2), null);
}
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
}
现在,当我启动活动时,它会按要求工作,它会在单击屏幕的位置(以单击点为中心)创建一个位图。它给出的问题是,当我按下手机上的返回键时,应用程序停止响应,并且手机提供了强制关闭应用程序的选项。我认为它必须与“ourThread”线程未正确连接有关。
知道问题出在哪里吗?谢谢。
编辑:我实际上已经找到了哪里出错了。在while循环中:
while(true){
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
canvas.drawRGB(255, 0, 0);
if(x!=0 && y!=0){
Bitmap ourBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green_ball);
canvas.drawBitmap(ourBitmap, x-(ourBitmap.getWidth()/2), y-(ourBitmap.getHeight()/2), null);
}
我只是将“true”更改为“isRunning”。现在线程确实结束了,因此,活动在按下后退按钮时关闭。感谢大家提出宝贵的建议。