2013-07-22 166 views
0

我試圖表現出子彈,當用戶觸摸屏幕的Android drawBitmap油漆

我在寫這裏的子彈

public Projectile(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     paint = new Paint(); 
     bulletBitmap = BitmapFactory.decodeResource(context.getResources(), 
                R.drawable.bullet); 
    } 

    public interface ProjectileListener { 
     public void onProjectileChanged(float delta, float angle); 
    } 

    public void setProjectileListener(ProjectileListener l) { 
     listener = l; 
    } 

    public void setProjectileDirection(int x, int y, int size){ 
     pos = new Rect(x, y, size, size); 
     invalidate(); 
    } 

    protected void onDraw(Canvas c) { 
     c.drawBitmap(bulletBitmap, pos, pos, paint); 
     super.onDraw(c); 
    } 

,並在這裏把它

Projectile p = new Projectile(TowerAnimation.this); 
         p.setProjectileDirection(x, y, 50); 
         projectiles.add(p); 
         Canvas c = null; 
         p.onDraw(c); 

但是我得到這條線上的錯誤

c.drawBitmap(bulletBitmap, pos, pos, paint); 

我是否對drawBitmap做任何錯誤? 感謝

+0

你會得到什麼錯誤?您是否在以contxt爲參數的構造函數中初始化'bulletBitmap'? – Blackbelt

+0

java.lang.NullPointerException 07-22 16:03:23.231:E/AndroidRuntime(8347):\t at android.view.View.dispatchTouchEvent(View.java:5546) 07-22 16:03:23.231:E/AndroidRuntime(8347):\t at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951) 07-22 16:03:23.231:E/AndroidRuntime(8347):\t at android.view.ViewGroup.dispatchTouchEvent(ViewGroup .java:1712) 07-22 16:03:23.231:E/AndroidRuntime(8347):\t at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957) 這裏有一些其他 – NoobMe

+0

這是什麼:「Canvas c = null;」?你傳遞null給onDraw方法,可能會得到NullPointerException。 –

回答

1

在下面的代碼:

Projectile p = new Projectile(TowerAnimation.this); 
        p.setProjectileDirection(x, y, 50); 
        projectiles.add(p); 
        Canvas c = null; <------------------ here 
        p.onDraw(c);  <------------------ NPE 

要設置cnull並將它傳遞給onDraw()。這是正在發生的事情你onDraw()

protected void onDraw(Canvas c) { 
    null.drawBitmap(bulletBitmap, pos, pos, paint); <--------- NPE 
    super.onDraw(c); 
} 

編輯1:

我不知道你正在嘗試用你的代碼做。檢查類BulletsOnScreen。要使用它,您需要將其作爲視圖添加到某個佈局。例如,如果你有一個LinearLayout,你可以使用addView()方法:

myLinearLayout.addView(new BulletsOnScreen(this)); 

public class BulletsOnScreen extends View { 

    Bitmap bullet; 

    boolean touched; 

    float xValue, yValue; 

    public BulletsOnScreen(Context context) { 

     super(context); 

     setFocusable(true); 

     bullet = BitmapFactory.decodeResource(context.getResources(), 
               R.drawable.bullet); 

     touched = false; 

    } 

    protected void onDraw(Canvas canvas) { 

     if (touched) { 

      canvas.drawBitmap(bullet, xValue, 
      yValue, null); 

      touched = false; 

     } 
    } 

    public boolean onTouchEvent(MotionEvent event) { 

    xValue = event.getX(); 
    yValue = event.getY(); 

      touched = true; 
      invalidate(); 
    } 
+0

我試過這個Canvas c = new Canvas(); 但沒有運氣我的形象仍然沒有出現〜 – NoobMe

+0

@NoobMe檢查編輯1上面。 – Vikram

+0

你有什麼想法,爲什麼我不能讓我的圖像出現在屏幕上? :) – NoobMe