The title pretty much explains the issue.
I have a button that when I press toggles on/off this thread. It turns it on and off successfully… once, then it crashes if I try to turn it on again.
private Thread dataThread = new Thread(new Runnable(){
public void run() {
while(transmitPackets){
Log.d("Test","DERP");
}
}
});
and where its run…
toggleButton.setOnClickListener( new View.OnClickListener() {
public void onClick(View v){
transmitPackets = !transmitPackets;
if( transmitPackets ) {
toggleButton.setText("Pause");
dataThread.start();
}
else {
toggleButton.setText("Transmit");
}
}
});
transmitPackets is the boolean toggled by the button press. Named as such because this app will eventually be sending data over a network. The thread uses it to terminate as well.
The stack trace generated by the app crash isn’t particularly helpful in figuring out how to fix it to me as it just says it crashed on restarting the thread–which was evident by the problem itself.
I’m new to the Android SDK and threading in Java both so I don’t know where I could be going wrong. This seems to be the simplest implementation of a thread possible which is where I’m starting before I try to do anything funky with the thread.
You would need to move your
DataThreadcode into thesetOnClickListenercode, as you only create one thread, so when you ‘start’ it again, it can’t, as it is done. You should declareDataThreadwhere you do now, but set it in the listener