I made a simple screen with two flying bird animations using SurfaceView and Bitmap. I'm now trying to add an OnClickListener to each animation, I want different sounds to be played depending on which animation is clicked using SoundPool but for now, I have just added a toast message to see if the OnClickListener works.
Im not sure how to go about this, the method I am currently trying is that I have added two different ImageButtons in the XML file and attach those ImageButtons to my Bitmaps. I have added all the code but I think the problem lies in the draw() method.
Heres the Error I get
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setImageBitmap(android.graphics.Bitmap)' on a null object reference 
The error message links to the following line of code
imgBtnPurple.setImageBitmap(resizedPurpleBird);
Code
public class PlayActivity extends AppCompatActivity {
surfaceView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    view = new surfaceView(this);
    setContentView(view);
   private class Anim extends Thread {
        int counter = 0;
        int counter2 = 0;
        @Override
        public void run() {
            long last_updated_time = 0;
            long delay = 150;
            int[] purple_bird = {
                    R.drawable.bird1,
                    R.drawable.bird2
            };
            int[] red_bird = {
                    R.drawable.red1,
                    R.drawable.red2,
                    R.drawable.red3,
                    R.drawable.red4,
                    R.drawable.red5,
                    R.drawable.red6,
                    R.drawable.red7,
                    R.drawable.red8,
                    R.drawable.red9,
                    R.drawable.red10
            };
            while (true) {
                boolean playing = true;
                if (playing) {
                    long current_time = System.currentTimeMillis();
                    if (current_time > last_updated_time + delay) {
                        if (counter >= 2) {
                            counter = 0;
                        }
                        if (counter2 >= 4) {
                            counter2 = 0;
                        }
                        draw(purple_bird[counter], red_bird[counter2]);
                        last_updated_time = current_time;
                        counter++;
                        counter2++;
                    }
                }
            }
        }
        private void draw(int red_bird, int purple_bird) {
            SurfaceHolder holder = getHolder();
            Canvas canvas = holder.lockCanvas();
            if (canvas != null) {
                canvas.drawColor(Color.WHITE);
                Paint paint = new Paint();
                Bitmap purpleBird = BitmapFactory.decodeResource(getContext().getResources(), purple_bird);
                Bitmap redBird = BitmapFactory.decodeResource(getContext().getResources(), red_bird);
                Bitmap resizedRedBird = Bitmap.createScaledBitmap(redBird, (int) (redBird.getWidth() * 0.1), (int) (redBird.getHeight() * 0.1), true);
                Bitmap resizedPurpleBird = Bitmap.createScaledBitmap(purpleBird, (int) (purpleBird.getWidth() * 0.4), (int) (purpleBird.getHeight() * 0.4), true);
                canvas.drawBitmap(resizedRedBird, 100, 100, paint);
                canvas.drawBitmap(resizedPurpleBird, 100, 600, paint);
                holder.unlockCanvasAndPost(canvas);
                ImageButton imgBtnPurple = (ImageButton) findViewById(R.id.imageBtnPurple);
                imgBtnPurple.setImageBitmap(resizedPurpleBird);
                imgBtnPurple.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(),"Hello Purple Bird", Toast.LENGTH_SHORT).show();
                    }
                });
                ImageButton imgBtnRed = (ImageButton) findViewById(R.id.imageBtnRed);
                imgBtnRed.setImageBitmap(resizedRedBird);
                imgBtnRed.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(),"Hello Red Bird",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }
}
}
Layout Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PlayActivity">
<ImageButton
    android:id="@+id/imageBtnPurple"
    android:layout_width="58dp"
    android:layout_height="49dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.155"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.143"
    app:srcCompat="@drawable/bird1" />
<ImageButton
    android:id="@+id/imageBtnRed"
    android:layout_width="54dp"
    android:layout_height="45dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/imageBtnPurple"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/imageBtnPurple"
    app:layout_constraintTop_toBottomOf="@+id/imageBtnPurple"
    app:layout_constraintVertical_bias="0.332"
    app:srcCompat="@drawable/red1" />
</androidx.constraintlayout.widget.ConstraintLayout>
