我一直绞尽脑汁试图弄明白这一整天。我有足够的麻烦让数据库工作(它现在做)。我遇到的问题是我得到一个空指针异常。这是文件的重要部分(它的方式太大,无法发布整个文件。
db = new MyDatabase(activity);
attackCursor = db.getPlayerAttackWeapons();
attackButtons = new ButtonSprite[attackCursor.getColumnCount()];
if (attackCursor != null) {
attackCursor.moveToFirst();
while (attackCursor.isAfterLast() == false) {
attackButtons[attackButtonsCounter] =
new ButtonSprite(0, 0, PlayerMenu.getPlayerMenuAttackTR(),
engine.getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
if (pSceneTouchEvent.isActionDown()) {
beep.play();
attackType = attackCursor.getInt(0);
this.setVisible(false);
selectTargetMessage.setVisible(true);
gameScene.unregisterTouchArea(this);
playerMenu.setVisible(false);
this.setParent(playerMenuAttack);
}
return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
pTouchAreaLocalY);
}
};
attackButtons[attackButtonsCounter].setColor(Color.YELLOW);
attackButtons[attackButtonsCounter].setPosition(0,attackButtonsYpos - 0-attackButtons[attackButtonsCounter].getHeight());
attackButtonsYpos = attackButtonsYpos - 90;
attackButtonsCounter++;
attackCursor.moveToNext();
}
}
现在,这部分代码没有任何错误,但是这应该会在屏幕上产生可见的按钮。它不是。此外,nullpointerexception出现在代码的这一部分:
playerMenuAttack = new Sprite(0, 0, PlayerMenu.getPlayerMenuAttackTR(),
engine.getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
if (pSceneTouchEvent.isActionDown()) {
beep.play();
int i = 0;
while (i <= attackButtons.length)
{
//getButtonTogglers(attackButtons[i]);
attackButtons[i].setVisible(true);
i++;
}
}
return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
pTouchAreaLocalY);
}
};
playerMenuAttack.setPosition(0,0);
当我点击playerMenuAttack sprite时,我得到了nullpointer异常。它指向包含attackButtons [i] .setVisible(true)的行;我完全失去了......