2011-01-08 44 views
0

我對Java和android非常陌生。我的第一個應用程序使用畫布和油漆出於某種原因,當我嘗試使用drawText方法時,我會得到一個關閉力。請幫助。 我基本上試圖在特定的x,y座標中顯示文本。這將需要更新整個遊戲,我的代碼:drawText畫布方法不起作用

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... 

您的幫助表示讚賞。

回答

1

初始化數p作爲跟隨

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

然後用

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

您的畫圖對象「p」永遠不會被創建。它包含空指針,因此你得到的異常。