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)
Log.d("Thread", "mThreadActive is false!");
if(c == null)
Log.d("Thread", "c is null!");
doDraw(c);
}
} 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)
doDraw(c);
}
} finally {
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
, API 15, API 10?
: SurfaceView , !
? , ( )?
.