从另一个 class 检查 ToogleButton 的状态
Check the state of a ToogleButton from another class
我想知道如何让我的应用程序检查我的切换按钮是否被选中,以便当我按下播放按钮时,应用程序只为选中的切换按钮播放声音。 (例如,如果选中按钮 1 和 3,当我按下播放按钮时,它应该执行 "Sound, silence, sound, silence" 并循环直到我按下停止)。
问题是,现在当我按下播放键时,我的应用程序关闭并且出现错误 "Attempt to invoke virtual method 'boolean android.widget.ToggleButton.isChecked()' on a null object reference"
。
我不知道如何解决这个问题。这是我的代码:
主要活动:
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();
}
});
}
采样器 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;
}
在Sampler
class中,button1
没有初始化,导致null object reference
错误。要 link 到 MainActivity 的 button1
,试试这个:
button1 = (((Activity)mycontext).findViewById(R.id.button1));
我想知道如何让我的应用程序检查我的切换按钮是否被选中,以便当我按下播放按钮时,应用程序只为选中的切换按钮播放声音。 (例如,如果选中按钮 1 和 3,当我按下播放按钮时,它应该执行 "Sound, silence, sound, silence" 并循环直到我按下停止)。
问题是,现在当我按下播放键时,我的应用程序关闭并且出现错误 "Attempt to invoke virtual method 'boolean android.widget.ToggleButton.isChecked()' on a null object reference"
。
我不知道如何解决这个问题。这是我的代码:
主要活动:
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();
}
});
}
采样器 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;
}
在Sampler
class中,button1
没有初始化,导致null object reference
错误。要 link 到 MainActivity 的 button1
,试试这个:
button1 = (((Activity)mycontext).findViewById(R.id.button1));