java - Android: thread exiting with uncaught exception -
i have got problem , can't find solution it. when receive data on bluetooth connection, program crashes. receiving data in separate thread each received byte saved in queue. have 1 timertask, calls method read data queue every 1 ms, , refresh textbox in ui. program works while, crashes. please help. here main code snippets.
public class mainactivity extends activity { textview mylabel1; volatile boolean stopworker = false; public queue<byte> receiverqueue = new linkedlist<byte>(); protected byte[] receiverarray; private boolean newdataread = false; //timer timertask mtimertask; timer timer = new timer(); final handler timerhandler = new handler(); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mylabel1 = (textview)findviewbyid(r.id.textview1); //start timer ticking after 1 sec, , give timer tick every 1ms ontimertick(); timer.schedule(mtimertask, 1000, 1); } //method reading data receiverqueue protected void readdatafromreceiverbuffer() { //if receiverqueue not empty, read data if(receiverqueue.isempty() == false) { //declaration of byte array receiverfield size of receiverqueue receiverarray = new byte[receiverqueue.size()]; //read bytes queue until empty (int = 0; receiverqueue.isempty() == false; i++) { //retrieves , removes head of queue, or returns null receiverarray[i] = receiverqueue.poll(); } //variable newdata set true, when read new data queue newdataread = true; } } thread datareceivethread = new thread(new runnable() { //received byte int receiveddata; public void run() { while(!thread.currentthread().isinterrupted() && !stopworker) { try { //read 1 byte inputstream receiveddata = mminstream.read(); //inserts received byte queue if possible if(receiveddata != -1) receiverqueue.offer((byte)receiveddata); } catch (ioexception ex) { stopworker = true; } //log.i(string.valueof(receiveddata), "valueofreceiveddata"); } } }); public void ontimertick() { mtimertask = new timertask() { string temp; //this method called public void run() { //call method read data queue readdatafromreceiverbuffer(); timerhandler.post(new runnable() { public void run() { //when new data has arrived, update ui if(newdataread == true) { //convert byte array string temp = new string(receiverarray, 0, receiverarray.length); mylabel1.settext(temp); newdataread = false; } } }); }}; }
logcat:
w/dalvikvm(12358): threadid=11: thread exiting uncaught exception (group=0x40ab1210) e/androidruntime(12358): fatal exception: timer-0 e/androidruntime(12358): java.lang.arrayindexoutofboundsexception: length=21; index=21 e/androidruntime(12358): @ com.example.colibri2bb.mainactivity.readdatafromreceiverbuffer(mainactivity.java:195) e/androidruntime(12358): @ com.example.colibri2bb.mainactivity.readdatafromreceiverbuffer(mainactivity.java:195) e/androidruntime(12358): @ com.example.colibri2bb.mainactivity$3.run(mainactivity.java:230) e/androidruntime(12358): @ java.util.timer$timerimpl.run(timer.java:284)
logcat:
w/dalvikvm(17344): threadid=12: thread exiting uncaught exception (group=0x40ab1210) e/androidruntime(17344): fatal exception: timer-1 e/androidruntime(17344): java.util.nosuchelementexception e/androidruntime(17344): atjava.util.linkedlist.removefirstimpl(linkedlist.java:689) e/androidruntime(17344): @ java.util.linkedlist.removefirst(linkedlist.java:676) e/androidruntime(17344): @ java.util.linkedlist.poll(linkedlist.java:895) e/androidruntime(17344):atcom.example.colibri2bb.mainactivity.readdatafromreceiverbuffer(mainactivity.java:196) e/androidruntime(17344): atcom.example.colibri2bb.mainactivity$3.run(mainactivity.java:231) e/androidruntime(17344): @ java.util.timer$timerimpl.run(timer.java:284)
could other thread modifying receiverqueue , adding data while you're reading it? try using loop instead. should make sure don't go out of bounds.
int queuesize = receiverqueue.size(); receiverarray = new byte[queuesize ]; (int = 0; i<queuesize; i++) { ... }
Comments
Post a Comment