对于音乐应用程序(音序器),我有几个按钮
static ArrayList<Button> Buttonlist = new ArrayList<Button>();
Buttonlist.add(0,(Button) findViewById(R.id.case_kick1));
Buttonlist.add(1,(Button) findViewById(R.id.case_kick2));
Buttonlist.add(2,(Button) findViewById(R.id.case_kick3));
Buttonlist.add(3,(Button) findViewById(R.id.case_kick4));
Buttonlist.add(4,(Button) findViewById(R.id.case_kick5));
Buttonlist.add(5,(Button) findViewById(R.id.case_kick6));
Buttonlist.add(6,(Button) findViewById(R.id.case_kick7));
Buttonlist.add(7,(Button) findViewById(R.id.case_kick8));
例如,我通过 onClickListeners 激活
Buttonlist.get(0).setActivated(true);
我通过循环和 ifs 播放了激活的声音:
if (Buttonlist.get(k).isActivated()) {mSoundManager.playSound(1); Thread.sleep(250);}
该应用程序运行良好,但在播放时我无法访问播放/暂停按钮:我搜索并发现了 AsyncTasks。
我有一个嵌套类 PlayPause :
class PlayPause extends AsyncTask<ArrayList<Button>,Integer,Void>
我有这个:
protected Void doInBackground(ArrayList<Button>... params) {
for(int k=0;k<8;k++)
{
boolean isPlayed = false;
if (Buttonlist.get(k).isActivated()) {
mSoundManager.playSound(1);
isPlayed = true;
try {Thread.sleep(250);
}
catch (InterruptedException e)
{e.printStackTrace();}}
if(!isPlayed){
try {Thread.sleep(250);}
catch (InterruptedException e)
{e.printStackTrace();}
}
}
return null;
}
我通过启动它
Play.setOnClickListener(new PlayClickListener());
使用 PlayClickListener :
public class PlayClickListener implements OnClickListener {
private Tutorial acti;
public PlayClickListener(){
super();
}
@SuppressWarnings("unchecked")
public void onClick(View v) {
if(Tutorial.play==0){
Tutorial.play=1;
Tutorial.Play.setBackgroundResource(R.drawable.action_down);
acti = new Tutorial();
Tutorial.PlayPause myPlayPause = acti.new PlayPause();
myPlayPause.execute(Tutorial.Buttonlist);
}
else
{
Tutorial.play=0;
Tutorial.myPlayPause.cancel(true);
Tutorial.Play.setBackgroundResource(R.drawable.action);
}
}
}
但它不起作用。当我点击按钮并点击播放/暂停时,我有这个:
07-02 11:06:01.350: E/AndroidRuntime(7883): FATAL EXCEPTION: AsyncTask #1
07-02 11:06:01.350: E/AndroidRuntime(7883): Caused by: java.lang.NullPointerException
07-02 11:06:01.350: E/AndroidRuntime(7883): at com.Tutorial.Tutorial$PlayPause.doInBackground(Tutorial.java:603)
07-02 11:06:01.350: E/AndroidRuntime(7883): at com.Tutorial.Tutorial$PlayPause.doInBackground(Tutorial.java:1)
而且我不知道如何摆脱这个错误。显然,即使列表是静态的,Asynctask 也没有找到 Buttonlist 的激活状态。我不知道如何访问这些按钮的状态,isPressed 也不起作用。
感谢您阅读和帮助我!