有时当我测试我的应用程序时,我会在下面提到的行中收到空指针异常错误。logcat和代码如下。请帮我解决这个错误。
canvas.drawBitmap(backgoundImage, 0, 0 , null);
当应用程序崩溃时,我检查了图像的大小以检查 variable-backgoundImage 是否为空。它正确显示尺寸。我怀疑画布所涉及的逻辑。
代码:
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);
}
}
日志猫:
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