这是comple 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);
            }
        }
    }
}

现在,当我开始 Activity 时,它会按要求工作,它将在单击屏幕的位置(以点击点为中心)创建一个位图。它带来的问题是,当我按手机上的后退键时,应用程序停止响应,并且手机提供了强制关闭应用程序的选项。我认为它必须与“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”。现在该线程确实结束了,因此,在按下“后退”按钮时该 Activity 将关闭。谢谢大家的宝贵建议。

最佳答案



您必须意识到,调用ourThread.join()仅等待线程完成-不会停止线程。您的ourThread可能由于某些原因而被挂起。线程转储将显示其运行位置。也许卡在一个循环中?也许正在等待网络连接?

如果join()成功返回,根据定义,被连接的线程不再运行。因此,调用ourThread.join()的线程将等待ourThread线程完成。

如果该线程不再运行,那么也许有其他线程在运行,从而使您的应用程序保持打开状态? thread dump将向您显示other non-daemon threads仍在附近。他们将需要终止,然后您的应用程序将停止。

在查看您的线程代码时,除非抛出RuntimeException,否则您看不到退出无限循环的任何方法。线程什么时候停止工作?您是要执行break;而不是continue;吗?

while(true){
    if(!ourHolder.getSurface().isValid())
        continue; // should this be a break?
    Canvas canvas = ourHolder.lockCanvas();
    canvas.drawRGB(255, 0, 0);
    ourHolder.unlockCanvasAndPost(canvas);
}

现在,您已经添加了更多代码,您需要将isRunning设置为volatile,以便多个线程可以更新它并查看更新,并且应该将while方法中的run()循环更改为:
private volatile boolean isRunning;
...
while(!isRunning){

10-06 00:27