从另一个类检查ToogleButton的状态

问题描述 投票:0回答:1

我想知道如何要求我的应用检查我的切换按钮是否已选中,以便当我按下播放按钮时,该应用仅针对已选中的切换按钮播放声音。 (例如,如果选中了按钮1和3,则当我按下“播放”按钮时,它应该执行“声音,静音,声音,静音”并循环播放,直到我按下“停止”为止)。

问题是,当我按Play时,我的应用程序关闭并且出现错误"Attempt to invoke virtual method 'boolean android.widget.ToggleButton.isChecked()' on a null object reference"

我不知道如何解决此问题。这是我的代码:

MainActivity:

public class MainActivity extends AppCompatActivity {


public ToggleButton button1, button2, button3, button4;
public Sampler mySample;
public ImageButton playButton;
public ImageButton stopButton;
public boolean playing;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    playButton = (ImageButton) findViewById(R.id.play);
    stopButton = (ImageButton) findViewById(R.id.stop);

    button1 = (ToggleButton) findViewById(R.id.button1);
    button2 = (ToggleButton) findViewById(R.id.button2);
    button3 = (ToggleButton) findViewById(R.id.button3);
    button4 = (ToggleButton) findViewById(R.id.button4);

    mySample = new Sampler(this);


    playButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            mySample.play();
        }
    });

    stopButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            mySample.stop();
        }
    });
}

采样器类:

public class Sampler extends Activity {

SoundPool sp;
private int snareId;
private Context mycontext;
public boolean playing;
public ToggleButton button1, button2, button3, button4;



public Sampler(Context appcontext) {

    this.mycontext = appcontext;

    sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

    snareId = sp.load(mycontext, R.raw.snare, 1);


}


public void play() {

    playing = true;
    while (playing) {
        if (button1.isChecked()) {
            sp.play(snareId, 10, 10, 1, 0, 1);
            try {
                Thread.sleep(60000 / (120 * 4));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (button2.isChecked()) {
            sp.play(snareId, 10, 10, 1, 0, 1);
            try {
                Thread.sleep(60000 / (120 * 4));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (button3.isChecked()) {
            sp.play(snareId, 10, 10, 1, 0, 1);
            try {
                Thread.sleep(60000 / (120 * 4));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (button4.isChecked()) {
            sp.play(snareId, 10, 10, 1, 0, 1);
            try {
                Thread.sleep(60000 / (120 * 4));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if(!playing){
                break;
        }
    }
}


public void stop(){

    playing = false;
}
android togglebutton
1个回答
0
投票

Sampler类中,未初始化button1,导致null object reference错误。要链接到MainActivity的button1,请尝试以下操作:

button1 = (((Activity)mycontext).findViewById(R.id.button1));
© www.soinside.com 2019 - 2024. All rights reserved.