0

i am trying to show bullets when the user touched the screen

i am making the bullet here

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);
    }

and call it here

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

however i am getting errors on this line

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

did i make anything wrong with the drawBitmap? thanks

NoobMe
  • 544
  • 2
  • 6
  • 26
  • what error do you get? Did you initialize `bulletBitmap` in the constructor that takes a contxt as paramter? – Blackbelt Jul 22 '13 at 07:53
  • java.lang.NullPointerException 07-22 16:03:23.231: E/AndroidRuntime(8347): at android.view.View.dispatchTouchEvent(View.java:5546) 07-22 16:03:23.231: E/AndroidRuntime(8347): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951) 07-22 16:03:23.231: E/AndroidRuntime(8347): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712) 07-22 16:03:23.231: E/AndroidRuntime(8347): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957) and here are some others – NoobMe Jul 22 '13 at 07:56
  • What for is this line: "Canvas c = null;"? You are passing null to onDraw method and probably getting NullPointerException. – Chiral Code Jul 22 '13 at 07:56
  • i guess i just put that because i dont know what to put so i can call p.onDraw – NoobMe Jul 22 '13 at 08:02

1 Answers1

1

In the following code:

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

You are setting c to null and passing it to onDraw(). This is what's happening in your onDraw():

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

Edit 1:

I'm not sure what you are trying to do with your code. Check the class BulletsOnScreen. To use it, you will need to add it as a View to some layout. For example, If you have a LinearLayout, you can use the addView() method:

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();
    }
Vikram
  • 51,313
  • 11
  • 93
  • 122