本文介绍了SurfaceView崩溃的surfaceChange的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经延伸SurfaceView和线程绘制在android系统在画布上的一些对象的例子

I have made an example that extend SurfaceView and Threads to draw some objects on canvas in android

但我面临两个问题

[1]当preSS后退按钮应用程序关闭,然后告诉我说:不幸的是,你的例子已经停止。

[1] When press back button the application closed and then show me that "Unfortunately, your example has stopped".

[2]当改变从纵向变为横向或VS方向,应用程序未能及时调整新的屏幕,并且不更新图纸那么它是崩溃,并显示了同样的信息很不幸,你的例子有停止。

[2] When change the orientation from "Portrait" to "Landscape" or VS , the application failed to adjust the new screen and does not update the drawing then it is crash and show the same message "Unfortunately, your example has stopped".

请查看code:

@Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO implement this method
        holder.getSurface().setSize(width, height);
        holder.setFormat(format);
    }

@Override
    public void surfaceCreated(SurfaceHolder holder) {
        // at this point the surface is created and
        // we can safely start the game loop
        thread.setRunning(true);
        thread.start();
    }

@Override
    public void surfaceDestroyed(SurfaceHolder holder) {

        // tell the thread to shut down and wait for it to finish
        // this is a clean shutdown
        boolean retry = true;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // try again shutting down the thread
            }
        }
    }

任何一个可以请帮助?

can any one please help ??

当我检查我发现下面的行logcat的,在这部分问题。

when I check the logcat I found the following line that the problem in this part

@Override
    public void run() {
        Canvas canvas;
        // initialise timing elements for stat gathering
        initTimingElements();

        long beginTime; // the time when the cycle begun
        long timeDiff; // the time it took for the cycle to execute
        int sleepTime; // ms to sleep (<0 if we're behind)
        int framesSkipped; // number of frames being skipped

        sleepTime = 0;

        while (running) {
            canvas = null;
            // try locking the canvas for exclusive pixel editing
            // in the surface
            try {
                canvas = this.surfaceHolder.lockCanvas();
                LoggerHandler.log("canvas : " + canvas);
                synchronized (surfaceHolder) {
                    beginTime = System.currentTimeMillis();
                    framesSkipped = 0; // resetting the frames skipped
                    // update game state
                    this.gamePanel.update();
                    // render state to the screen
                    // draws the canvas on the panel
                    this.gamePanel.render(canvas);
                    // calculate how long did the cycle take
                    timeDiff = System.currentTimeMillis() - beginTime;
                    // calculate sleep time
                    sleepTime = (int) (FRAME_PERIOD - timeDiff);

                    if (sleepTime > 0) {
                        // if sleepTime > 0 we're OK
                        try {
                            // send the thread to sleep for a short period
                            // very useful for battery saving
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {
                        }
                    }

                    while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
                        // we need to catch up
                        this.gamePanel.update(); // update without rendering
                        sleepTime += FRAME_PERIOD; // add frame period to check
                                                    // if in next frame
                        framesSkipped++;
                    }

                    // for statistics
                    framesSkippedPerStatCycle += framesSkipped;
                    // calling the routine to store the gathered statistics
                    storeStats();
                }
            } finally {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            } // end finally
        }
    }

在此行中的问题 - >帆布= this.surfaceHolder.lockCanvas();这是改变方向时,返回null。

The problem in this line --> canvas = this.surfaceHolder.lockCanvas(); it is return null when change the orientation .

推荐答案

我有同样的问题。解决方法:只检查非空画布。
在我的测试:

I had the same problem. Solution: just check canvas for non null.In my tests:

            try {
                canvas = surfaceHolder.lockCanvas(null);
                if(canvas != null){
                    synchronized (surfaceHolder) {
                        canvas.drawColor(Color.BLACK);
                        canvas.drawBitmap(picture, 0, 0 , null);
                    }
                }
            }
            finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }

这篇关于SurfaceView崩溃的surfaceChange的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:28
查看更多