0

I'm currently trying to show a score which requires the text to change dynamically in a game. I've searched around and found that most cases people use XML layout for the text. My problem is I don't use XML at all for the game because everything are bitmap graphics. Any tips or suggestion for my situation?

Here is the draw method to draw everything

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
}

Here is the method to update the score

private void updateScore(int score){
    initScore += score;
    totalScore = initScore;
    scoreString = Integer.toString(totalScore);
}

It returns NullPointerException at android.graphics.Canvas.drawText(Native Method). I tried logging the "scoreString" and it shows correctly.

Edit: Solved, NullPointerException caused by null paint object. Simply create paint object Paint paintObj = new Paint(); and set the object paintObj.setTextSize(textSize) and paintObj.setColor(Color.WHITE);

Steven Pongidin
  • 153
  • 1
  • 11

1 Answers1

2

If you are doing your drawing directly through a View or SurfaceView object, you may want to check the Canvas documentation:

http://developer.android.com/reference/android/graphics/Canvas.html

Specifically the draw text function. This is what I use.

http://developer.android.com/reference/android/graphics/Canvas.html#drawText(java.lang.String, float, float, android.graphics.Paint)

Enjoy!

If you're using an Open GL Surface, I'm not sure what APIs are available. On other platforms I've uploaded my characters as a texture atlas and just placed the textures for the correct text that I wanted on the scene.

nmjohn
  • 1,432
  • 7
  • 16
  • I actually found something like that http://stackoverflow.com/questions/6304195/android-displaying-score-as-a-string-on-canvas-is-creating-a-new-string-per-dr But I couldn't get it to work with error NullPointerException, I followed it perfectly btw. If that's the solution you mean, I'll try working on it. – Steven Pongidin Jan 03 '12 at 06:10
  • Does that setup work for you, or are you looking for a different solution? If it's the latter please explain a bit more about how you are developing your game and I can provide better solutions. – nmjohn Jan 03 '12 at 06:11
  • If you want to post your example code, I can help with debugging it. My guess it is either the canvas object isn't valid or the paint object isn't valid and I can try to help debug it either in this question or another if you link to it. – nmjohn Jan 03 '12 at 06:21
  • 1
    You definitely need to create a basic paint object - see my edit to your post and see if that fixes it. The paint object may be null for drawing bitmaps, but not text. – nmjohn Jan 03 '12 at 07:15
  • I've added `myPaint = new Paint();` and `myPaint.setColor(Color.WHITE);` but still the same error. I've also initialized `scoreString = null;` just in case. – Steven Pongidin Jan 03 '12 at 07:22
  • 1
    Did you also set canvas.drawText(scoreString, 300, 20, myPaint)? I think I made edits above but they are gone. Don't initialize scoreString to null, it needs to be a valid string like "100". – nmjohn Jan 03 '12 at 07:24
  • 1
    Oh it works! Sorry I accidentally changed your revision. Followed what you wrote there and it worked like a charm. Thanks a lot =) – Steven Pongidin Jan 03 '12 at 07:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6336/discussion-between-nmjohn-and-steven-pongidin) – nmjohn Jan 03 '12 at 07:29