現在、ゲームでテキストを動的に変更する必要があるスコアを表示しようとしています。私があちこち検索したところ、ほとんどの場合、テキストに XML レイアウトが使用されていることがわかりました。私の問題は、すべてがビットマップ グラフィックであるため、ゲームに XML をまったく使用していないことです。私の状況に対するヒントや提案はありますか?
すべてを描画する draw メソッドは次のとおりです
public void render(Canvas canvas){
Bitmap bitmap;
Graphics.Coordinate coords;
canvas.drawBitmap(bgBitmap, 0, 0, null);
canvas.drawBitmap(closeBtnBitmap, 700, 0, null);
canvas.drawBitmap(groundBitmap, 0, 315, null);
canvas.drawBitmap(petBitmap, petX, petY, null);
for(Graphics pics : coins){
bitmap = pics.getBitmap();
coords = pics.getCoord();
canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
}
canvas.drawBitmap(scoreBitmap, 300, 20, null);
canvas.drawText(scoreString, 300, 20, null); //change null to paintObj
}
スコアの更新方法はこちら
private void updateScore(int score){
initScore += score;
totalScore = initScore;
scoreString = Integer.toString(totalScore);
}
android.graphics.Canvas.drawText(Native Method) で NullPointerException を返します。「scoreString」をログに記録してみましたが、正しく表示されます。
編集: 解決済み、null ペイント オブジェクトが原因の NullPointerException。単純にペイント オブジェクトを作成しPaint paintObj = new Paint();
、オブジェクトを設定してpaintObj.setTextSize(textSize)
、paintObj.setColor(Color.WHITE);