我整天都在绞尽脑汁想弄清楚这一点。我在使数据库正常工作时遇到了很多麻烦(现在可以了)。我遇到的问题是我得到一个空指针异常。这是文件的重要部分(它太大而无法发布整个内容。
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 精灵时出现空指针异常。它指向包含 attackButtons[i].setVisible(true); 的行 我完全失去了......