AsyncTask, inner classes and Buttons states
Posted
by
Intern John Smith
on Stack Overflow
See other posts from Stack Overflow
or by Intern John Smith
Published on 2012-07-02T09:13:03Z
Indexed on
2012/07/02
9:15 UTC
Read the original article
Hit count: 210
For a musical application (a sequencer), I have a few buttons
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));
that I activate through onClickListeners with for example
Buttonlist.get(0).setActivated(true);
I played the activated sound via a loop and ifs :
if (Buttonlist.get(k).isActivated()) {mSoundManager.playSound(1); Thread.sleep(250);}
The app played fine but I couldn't access the play/pause button when it was playing : I searched and found out about AsyncTasks.
I have a nested class PlayPause :
class PlayPause extends AsyncTask<ArrayList<Button>,Integer,Void>
in which I have this :
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;
}
I launch it via
Play.setOnClickListener(new PlayClickListener());
with 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();
myLecture.execute(Tutorial.Buttonlist);
}
else
{
Tutorial.play=0;
Tutorial.lecture.cancel(true);
Tutorial.Play.setBackgroundResource(R.drawable.action);
}
}
}
But it doesn't work. when I click on buttons and I touch Play/Pause, I have this :
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)
And I don't know how to get rid of this error. Obviously, the Asynctask doesn't find the Buttonlist activated status, even if the list is static. I don't know how to access these buttons' states, isPressed doesn't work either.
Thanks for reading and helping me !
© Stack Overflow or respective owner