Unexpected NullPointerException in SurfaceView-Thread at API Level 15

I programmed an application that I wanted to test at higher API levels to check compatibility. There were no problems for API 10 (2.3.3), but as soon as I ran the application in API 15 (4.0.3), I got a NullPointerException in one of my SurfaceViews when I left Activity. I have to say that I solved the problem, but I can’t understand why the exception actually happened. So maybe you could tell me.

Here is the code that worked for me in API 10: This is the general structure of the method run().

public void run() {
    while (mThreadActive) {
        c = null;
        try {
            c = mSurfaceHolder.lockCanvas(null);
            synchronized (mSurfaceHolder) {
                if(mState == 1) {
                    updateValues();
                    updateAnimation();
                }
                doDraw(c);
            }
        } finally {
            if (c != null) {
                mSurfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}

API 15 : , doDraw() "c". c , , , . mThreadActive , , false, while -loop . :

public void run() {
    while (mThreadActive) {
        c = null;
        try {
            c = mSurfaceHolder.lockCanvas(null);
            synchronized (mSurfaceHolder) {
                if(mState == 1) {
                    updateValues();
                    updateAnimation();
                }

                if(!mThreadActive)    // so it really is!
                    Log.d("Thread", "mThreadActive is false!");

                if(c == null)   // so it is too!
                    Log.d("Thread", "c is null!");

                doDraw(c);   // error
            }
        } finally {
            if (c != null) {
                mSurfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}

, mThreadActive . , while -statement, , "c" null mSurfaceHolder.lockCanvas(null). , .

, c != null, :

public void run() {
    while (mThreadActive) {
        c = null;
        try {
            c = mSurfaceHolder.lockCanvas(null);
            synchronized (mSurfaceHolder) {
                if(mState == 1) {
                    updateValues();
                    updateAnimation();
                }
                if(c != null) // prevent drawing on c if c doesnt exist.
                    doDraw(c);
            }
        } finally {
            if (c != null) {
                mSurfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}

, API 15, API 10? : SurfaceView , ! ? , ( )?

.

+3
2

, while(), , , , mThreadActive . mThreadActive volatile? , .

, lockCanvas(null) . , , . API null in lockCanvas? ( SurfaceHolder ?)

, API SurfaceHolder.lockCanvas() , null:

null, . Callback.surfaceCreated, , .

API, , SurfaceHolder.Callback surfaceCreated(), , , .

+2

:

public void surfaceDestroyed(SurfaceHolder holder)  {
    isAttached = false;
    this.drawthread = null;
}

, , boolean isAttached, , while - nullexception . -, , . if (canvas != null), , .

- - . , , , holder.lockCanvas() null - , , false, Thread. ?

, Thread.join(), while, t23 > null, , nullexception.

, , , "" bool , ( ), , if (canvas != null), , . , !

edit: , API 17

0

All Articles