現在、ゲームでテキストを動的に変更する必要のあるスコアを表示しようとしています。 私は検索して、ほとんどの人がテキストにXMLレイアウトを使用していることを発見しました。 私の問題は、すべてがビットマップグラフィックスなので、ゲームでXMLを使用しないことです。 私の状況に関するヒントやご提案はありますか?ここでAndroid - ビットマップ上の動的テキスト
は、ここですべて
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
}
を描画するdrawメソッドであることがandroid.graphics.Canvas.drawTextでNullPointerExceptionが(ネイティブメソッド)を返すスコア
private void updateScore(int score){
initScore += score;
totalScore = initScore;
scoreString = Integer.toString(totalScore);
}
を更新する方法です。私は "scoreString"を記録しようとしましたが、正しく表示されます。
編集:解決済み、NullPointerExceptionはnullペイントオブジェクトによって発生します。単にあなたが表示またはSurfaceViewオブジェクトを介して直接描画を行っている場合は、キャンバスのドキュメントをチェックすることもできますペイントオブジェクトPaint paintObj = new Paint();
を作成し、オブジェクトpaintObj.setTextSize(textSize)
とpaintObj.setColor(Color.WHITE);
キャンバスを使用してテキストを表示することができます。ペイントp =新しいペイント(); p.setColor(Color.BLUE); canvas.drawText( "あなたの文字列"、10、10、p); –
@HirenDabhiこのソリューションを使用しましたが、エラーが返されました。私は自分のコードを追加しました。 –
あなたのコードでは、nullをdrawText(最後のパラメータ)に渡しています。新しいPaint()を渡します。 –