0

I am very new to Java and android. my 1st app using canvas and paint. for some reason I get a force close whenever I try using the drawText method.. Please help. I am basically trying to display text in a specific x,y coordinate. which will need updates throughout game play, my code:

public class MyGame extends Main { TextView timeDisplay; public String clock; int x_pos = 10; int y_pos = 100; int radius = 20; float x = 10; float y = 20; android.graphics.Paint p; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); super.onCreate(savedInstanceState); setContentView(R.layout.main); // setup Drawing view Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); c.drawText("test", 30, 0,x,y, p); <-- if I comment this out, no force close... 

Your help is appreciated.

2 Answers 2

3

Your Paint object "p" is never created. It contains the null pointer, hence the exception you are getting.

Sign up to request clarification or add additional context in comments.

Comments

1

initialize p as follow

Paint p = new Paint(); p.setColor(Color.WHITE); p.setStyle(Style.FILL); 

and then use

c.drawText("test", 30, 0,x,y, p); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.