Check the state of a ToogleButton from another class

Michel Pinto :

I would like to know how can I ask my app to check if my toggle buttons are checked or not so that when I press the play button, the app plays a sound only for the toggle buttons that are checked. (For instance, if button 1 and 3 are checked, when I press the play button it should do "Sound, silence, sound, silence" and loop until I press stop).

The problem is that right now when I press play, my app closes and I get the error "Attempt to invoke virtual method 'boolean android.widget.ToggleButton.isChecked()' on a null object reference".

I don't know how to resolve this problem. Here is my code :

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

Sampler class :

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;
}
lurker0152 :

In Sampler class, button1 isn't initialised, resulting in null object reference error. To link up to MainActivity's button1, try this:

button1 = (((Activity)mycontext).findViewById(R.id.button1));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174770&siteId=1