有时,我在行中得到 NullPointerException。请让我知道如何纠正这个问题。在出错的时候,我已经检查了变量 backgoundImage 中空值的可能性。变量背景图像不为空。
canvas.drawBitmap(backgoundImage, 0, 0 , null);
代码:
@Override
public void run() {
// TODO Auto-generated method stub
ourHolder = getHolder();
while (isRunning) {
if (!ourHolder.getSurface().isValid()){
continue;
}
canvas = ourHolder.lockCanvas();
screenCenterX = dWidth / 2;
screenCenterY = dHeight / 2;
//-----------------------------------------------------------------------------------
if(backgoundImage == null){
try {
Log.i("DragDropCheck", "----------------backgoundImage is null--------");
backgoundImage = getAssetImage(getContext(),"backgroundhomepage");
canvas.drawBitmap(backgoundImage, 0, 0 , null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
canvas.drawBitmap(backgoundImage, 0, 0 , null);
}
//-----------------------------------------------------------------------------------
if (imagePublishDone) {
if(!welcomeDone){
welcomeDone=true;
}
moveImageInEllipticalPath();
} else {
initialImagePublish();
}
centreReached = false;
ourHolder.unlockCanvasAndPost(canvas);
}
}
日志猫:
07-09 22:40:18.689: E/AndroidRuntime(8794): FATAL EXCEPTION: Thread-621
07-09 22:40:18.689: E/AndroidRuntime(8794): java.lang.NullPointerEception
07-09 22:40:18.689: E/AndroidRuntime(8794): at com.eample.funandlearn.DragDrop$MyBringBackSurface.run(DragDrop.java:645)
07-09 22:40:18.689: E/AndroidRuntime(8794): at java.lang.Thread.run(Thread.java:856)