绘制背景时应用程序崩溃 - 空指针异常

时间:2013-07-08 04:22:36

标签: android performance android-canvas android-image

有时当我测试我的APP时,我在下面提到的行中得到空指针异常错误。 logcat和代码如下。请帮我解决这个错误。

canvas.drawBitmap(backgoundImage, 0, 0  , null);   

当应用程序崩溃时,我检查了图像的大小,以检查变量-backoundImage是否为空。它正确显示尺寸。我怀疑画布中涉及的逻辑。

代码:

    public void run() {
        // TODO Auto-generated method stub
//      Log.i("Notice", "In run of mybringback");  
        if(backgoundImage == null){ 
            try {Log.i("DragDrop", "backgoundImage is null.. In run of mybringback.. getting the image of background.."); 
                backgoundImage = getAssetImage(getApplicationContext(),"backgroundhomepage");  
                System.gc();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
            Log.i("DragDrop", "backgoundImage.getHeight() ;" + backgoundImage.getHeight());     

        ourHolder = getHolder();
        while (isRunning) {
//          Log.i("DragDrop", "ourHolder.getSurface().isValid()" +  ourHolder.getSurface().isValid() );
            if (!ourHolder.getSurface().isValid()){
                continue;
            } 
            canvas = ourHolder.lockCanvas();    
            screenCenterX = dWidth / 2;
            screenCenterY = dHeight / 2;  
            canvas.drawBitmap(backgoundImage, 0, 0  , null);   
            if (imagePublishDone) {
                moveImageInEllipticalPath();
            } else {
                initialImagePublish();
            }

            centreReached = false;
            ourHolder.unlockCanvasAndPost(canvas);
        }
    } 

LogCat:

07-08 09:42:07.639: I/DragDrop(30560): In Resume drag drop
07-08 09:42:07.659: I/mybringback(30560): In resume of mybringback
07-08 09:42:07.669: I/DragDrop(30560): backgoundImage.getHeight() ;320
07-08 09:42:07.669: W/dalvikvm(30560): threadid=12: thread eiting with uncaught eception (group=0
07-08 09:42:07.669: E/AndroidRuntime(30560): FATAL EXCEPTION: Thread-1090
07-08 09:42:07.669: E/AndroidRuntime(30560): java.lang.NullPointerEception
07-08 09:42:07.669: E/AndroidRuntime(30560): at com.eample.funandlearn.DragDrop$MyBringBackSurface.run(DragDrop.java:642)
07-08 09:42:07.669: E/AndroidRuntime(30560): at java.lang.Thread.run(Thread.java:856)
07-08 09:42:07.719: I/mybringback(30560): In Pause of mybringback

1 个答案:

答案 0 :(得分:0)

请删除此行并检查。 Log.i(“DragDrop”,“backgoundImage.getHeight();”+ backgoundImage.getHeight());