AndEngine Font loading:NullPointerException

时间:2014-05-12 10:45:44

标签: java android nullpointerexception andengine

好的,我一直在关注这个教程并实现我的游戏。但是,我在字体对象上得到一个空指针异常。我已经在assets / font文件夹中加载了font.ttf文件,这里是代码。请帮助。感谢...

在我的gameScene课程中:

      private void createHUD()
 {
      gameHUD = new HUD();
      scoreText = new Text(20, 420, resourcesManager.font, "Score: 0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
      scoreText = new Text(20, 420,mFont, "Score: 0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
      scoreText.setSkewCenter(0, 0);    
      scoreText.setText("Score: 0");
      gameHUD.attachChild(scoreText);
      camera.setHUD(gameHUD);
}

在我的ResourcesManager类中:

     private void loadMenuFonts()
    {
     FontFactory.setAssetBasePath("font/");
         final ITexture mainFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);

     font = FontFactory.createStrokeFromAsset(activity.getFontManager(), mainFontTexture, activity.getAssets(), "font.ttf", 50, true, Color.WHITE, 2, Color.BLACK);
     font.load();
     }

和logcat:

     05-12 15:57:37.478: E/AndroidRuntime(20431): java.lang.NullPointerException:    Attempt to invoke interface method 'org.andengine.opengl.font.Letter   org.andengine.opengl.font.IFont.getLetter(char)' on a null object reference
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.opengl.font.FontUtils.measureText(FontUtils.java:95)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.opengl.font.FontUtils.measureText(FontUtils.java:70)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.opengl.font.FontUtils.measureText(FontUtils.java:49)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.setText(Text.java:201)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.<init>(Text.java:159)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.<init>(Text.java:146)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.<init>(Text.java:138)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.<init>(Text.java:110)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.<init>(Text.java:102)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at com.packagename.scene.GameScene.createHUD(GameScene.java:55)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at com.packagename.scene.GameScene.createScene(GameScene.java:39)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at com.packagename.base.BaseScene.<init>(BaseScene.java:36)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at com.packagename.scene.GameScene.<init>(GameScene.java:26)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at com.packagename.manager.SceneManager$1.onTimePassed(SceneManager.java:112)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.engine.handler.timer.TimerHandler.onUpdate(TimerHandler.java:98)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.engine.handler.UpdateHandlerList.onUpdate(UpdateHandlerList.java:47)

2 个答案:

答案 0 :(得分:0)

尝试

BitmapTextureAtlas=new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

而不是

final ITexture mainFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);

答案 1 :(得分:0)

好的,终于得到了错误!错误是在GameScene类中创建文本时,必须从ResourceManager类访问该字体。但由于未调用函数loadMenuFonts(),因此未加载字体。因此,将该代码片段移动到loadGameFonts()函数中可确保在调用函数loadGameResources()函数时调用loadGameFonts()函数。

     public void loadGameResources()
{
    loadGameGraphics();
    loadGameFonts();
    loadGameAudio();
}

loadGameFonts()函数:

     private void loadGameFonts()
{
    FontFactory.setAssetBasePath("font/");
    final ITexture mainFontTexture = new   BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
    font = FontFactory.createStrokeFromAsset(activity.getFontManager(), mainFontTexture, activity.getAssets(), "font.ttf", 50, true, Color.WHITE, 2, Color.BLACK);
    //mainFontTexture.load();
    font.load();
}