3

我的绘图应用程序有问题。当我离开绘图活动(例如使用返回按钮)时,出现以下错误。我看过帖子,但似乎没有任何帮助。拜托,如果你能看看我的绘图表面活动,那将意味着很多,因为我一直在努力让它工作一段时间!非常感谢 :)

错误 :

03-25 13:41:36.760: E/AndroidRuntime(6642): FATAL EXCEPTION: Thread-423
03-25 13:41:36.760: E/AndroidRuntime(6642): java.lang.IllegalArgumentException
03-25 13:41:36.760: E/AndroidRuntime(6642):     at android.view.Surface.nativeUnlockCanvasAndPost(Native Method)
03-25 13:41:36.760: E/AndroidRuntime(6642):     at android.view.Surface.unlockCanvasAndPost(Surface.java:457)
03-25 13:41:36.760: E/AndroidRuntime(6642):     at android.view.SurfaceView$4.unlockCanvasAndPost(SurfaceView.java:812)
03-25 13:41:36.760: E/AndroidRuntime(6642):     at com.ecp.drawing.DrawingSurface$DrawThread.run(DrawingSurface.java:80)

DrawingSurface 的主要代码:

    public void run() {
        Canvas canvas = null;
        while (_run){
            if(isDrawing == true){
                try{
                    canvas = mSurfaceHolder.lockCanvas(null);
                    if(mBitmap == null){
                        mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                    }
                    final Canvas c = new Canvas (mBitmap);

                    c.drawColor(0, PorterDuff.Mode.CLEAR);
                    canvas.drawColor(0, PorterDuff.Mode.CLEAR);


                    commandManager.executeAll(c,previewDoneHandler);
                    previewPath.draw(c);

                    canvas.drawBitmap (mBitmap, 0,  0,null);
                } finally {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }


            }

        }

    }

已经有一个表面破坏功能:

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    boolean retry = true;
    thread.setRunning(false);
    while (retry) {
        try {
            thread.join();
            retry = false;
        } catch (InterruptedException e) {
            // we will try it again and again...
        }
    }
}
4

2 回答 2

2

我在 4.3 中遇到了类似的问题,在阅读了一段时间其他开发人员如何绕过这个错误之后,我来到了这个问题。

要记住的步骤:lockCanvasdraw然后unlockCanvas

/**
 * Note: The drawing thread doesn't loop, it just runs once and exits
 */
@Override
public void run() {
    /* This should never happen but just to be sure... */
    if (mSurfaceHolder == null || mSurfaceView == null) {
        return;
    }

    /**
     * In order to work reliable on Nexus 7, we place ~500ms delay at the start of drawing thread
     * (AOSP - Issue 58385)
     */
    if (android.os.Build.BRAND.equalsIgnoreCase("google") &&
            android.os.Build.MANUFACTURER.equalsIgnoreCase("asus") &&
            android.os.Build.MODEL.equalsIgnoreCase("Nexus 7")) {

        Log.w(this, "Sleep 500ms (Device: Asus Nexus 7)");

        try {
            Thread.sleep(500);
        } catch (InterruptedException ignored) {
        }
    }

    Canvas canvas = null;

    while (mRunning) {
        try {
            Surface surface = mSurfaceHolder.getSurface();

            /* Check availability of surface */
            if (surface != null && surface.isValid()) {

                canvas = mSurfaceHolder.lockCanvas();

                synchronized (mSurfaceHolder) {
                    if (canvas != null) {

                        //TODO call drawing code
                    }
                }
            }

        } catch (Exception e) {
            Log.e(TAG, "[Drawing Thread]", e);

        } finally {
            /**
             * Do this in a finally so that if an exception is thrown during the above,
             * we don't leave the Surface in an inconsistent state
             */
            if (canvas != null && mSurfaceHolder != null) {
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

我希望这可以帮助某人。;)

于 2015-02-25T19:49:35.310 回答
2

我猜这lockCanvas引发了一个异常,该异常被canvas设置为空。该异常将执行发送到finally调用的子句,该子句unlockCanvasAndPost引发了一个新异常。新的例外掩盖了旧的例外。

(这看起来有点像 4.3 中的错误,但这是在 4.3 出来之前发布的。)

于 2013-08-23T19:48:29.170 回答