首页 > 解决方案 > Android Studio 中位图动画上的 OnClickListener 抛出 NullPointerException

问题描述

我使用 SurfaceView 和 Bitmap 制作了一个带有两个飞鸟动画的简单屏幕。我现在正在尝试为每个动画添加一个 OnClickListener,我希望根据使用 SoundPool 单击哪个动画来播放不同的声音,但现在,我刚刚添加了一条 toast 消息以查看 OnClickListener 是否有效。

我不知道该怎么做,我目前正在尝试的方法是我在 XML 文件中添加了两个不同的 ImageButtons 并将这些 ImageButtons 附加到我的位图。我已经添加了所有代码,但我认为问题在于 draw() 方法。

这是我得到的错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setImageBitmap(android.graphics.Bitmap)' on a null object reference 

错误消息链接到以下代码行

imgBtnPurple.setImageBitmap(resizedPurpleBird);

代码

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


            }



        }


    }

}

}

布局代码

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

标签: javaandroidandroid-studiobitmaponclicklistener

解决方案


推荐阅读