图像不会从资产加载

时间:2014-01-12 01:30:43

标签: java android nullpointerexception assets android-image

我正在从“开始Android游戏”一书中编写一款安卓游戏。这场比赛被称为Nom先生,其基本上就像Snake。我完成了它的编写,它按预期工作,但是书中的代码只允许左右转动,有两个按钮(左箭头和右箭头)来完成该任务。我想编辑代码以允许您打开或关闭另外两个按钮(向上箭头和向下箭头)。

随书下载的源代码包含了一系列与此特定游戏同步的图像。其中一个图像包括左箭头和右箭头,所以我只是在Paint.Net中打开图像并将其旋转90度,使箭头朝上而不是从左到右。所有这些图像都存储在assets文件夹中,我将旋转后的图像保存为buttonsrotate.png。这是将按钮绘制到屏幕的代码:

private void drawRunningUI() {
    Graphics g = game.getGraphics();
    g.drawPixmap(Assets.buttons, 0, 0, 64, 128, 64, 64);
    g.drawLine(0, 416, 480, 416, Color.BLACK);
    g.drawPixmap(Assets.buttons, 0, 416, 64, 64, 64, 64);
    g.drawPixmap(Assets.buttons, 256, 416, 0, 64, 64, 64);
    g.drawPixmap(Assets.buttonsrotate, 64, 416, 64, 0, 64, 64);
    g.drawPixmap(Assets.buttons, 192, 416, 0, 64, 64, 64);
}

当我运行应用程序时,加载屏幕将会打开,但只要我触摸屏幕开始游戏,它就会停止响应并关闭。只有当我包含以下行时才会这样做:

    g.drawPixmap(Assets.buttonsrotate, 64, 416, 64, 0, 64, 64);

否则游戏运行良好。我确定图像在资产中,所以我不知道导致问题的原因。这是我用g.drawPixmap运行游戏时获得的logcat(Assets.buttonsrotate,64,416,64,0,64,64);代码中的一行:

01-11 20:25:50.625: D/ActivityThread(9344): setTargetHeapIdealFree:8388608
01-11 20:25:50.625: D/ActivityThread(9344): setTargetHeapConcurrentStart:2097152
01-11 20:25:50.695: V/SoundPoolThread(9344): beginThread
01-11 20:25:50.695: V/SoundPoolThread(9344): run
01-11 20:25:50.715: E/SensorManager(9344): thread start
01-11 20:25:50.725: D/SensorManager(9344): registerListener :: handle = 4  name= MPL accel    delay= 20000 Listener=     com.badlogic.androidgames.framework.impl.AccelerometerHandler@42b2bd30
01-11 20:25:50.815: I/Adreno200-EGLSUB(9344): : Format RGBA_8888.
01-11 20:25:50.855: E/(9344): : Can't open file for reading
01-11 20:25:50.855: E/(9344): : Can't open file for reading
01-11 20:25:50.985: V/SoundPoolThread(9344): Got message m=2, mData=1
01-11 20:25:50.995: V/MediaPlayer(9344): decode(57, 219601, 9225)
01-11 20:25:51.115: V/SoundPoolThread(9344): Got message m=2, mData=2
01-11 20:25:51.115: V/MediaPlayer(9344): decode(72, 228870, 10222)
01-11 20:25:51.205: V/SoundPoolThread(9344): Got message m=2, mData=3
01-11 20:25:51.205: V/MediaPlayer(9344): decode(74, 153262, 11595)
01-11 20:25:54.709: W/dalvikvm(9344): threadid=14: thread exiting with uncaught exception (group=0x41e41438)
01-11 20:25:54.719: E/AndroidRuntime(9344): FATAL EXCEPTION: Thread-6797
01-11 20:25:54.719: E/AndroidRuntime(9344): java.lang.NullPointerException
01-11 20:25:54.719: E/AndroidRuntime(9344):     at com.badlogic.androidgames.framework.impl.AndroidGraphics.drawPixmap(AndroidGraphics.java:104)
01-11 20:25:54.719: E/AndroidRuntime(9344):     at com.badlogic.androidgames.mrnom.GameScreen.drawRunningUI(GameScreen.java:194)
01-11 20:25:54.719: E/AndroidRuntime(9344):     at com.badlogic.androidgames.mrnom.GameScreen.present(GameScreen.java:134)
01-11 20:25:54.719: E/AndroidRuntime(9344):     at com.badlogic.androidgames.framework.impl.AndroidFastRenderView.run(AndroidFastRenderView.java:39)
01-11 20:25:54.719: E/AndroidRuntime(9344):     at java.lang.Thread.run(Thread.java:856)

请帮忙!它让我疯狂!!

1 个答案:

答案 0 :(得分:0)

好的,我用google搜索了GameFramework。您是否在LoadingScreen.java文件中添加了图像的路径?您可以看到这是针对所有其他资产完成的。所以添加一行如:

Assets.buttonsrotate = g.newPixmap("buttonsrotate.png", PixmapFormat.ARGB4444);

这应该可以解决问题。