0

有时,我在行中得到 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)
4

1 回答 1

0

可能问题在于canvas变量是null. ourHolder.lockCanvas()您应该管理该特定情况以避免在调用返回时执行代码(此外,您应该在执行循环之前null检查您的变量不为空)。ourHolderwhile

于 2013-07-09T17:27:39.060 回答