2

我有问题,我找不到解决方案。当我通过蓝牙连接接收数据时,我的程序突然崩溃。我在单独的线程中接收数据,每个接收到的字节都保存在队列中。我还有一个 TimerTask,它每 1 毫秒调用一次从队列中读取数据的方法,并在 UI 中刷新 TextBox。我的程序运行了一段时间,然后突然崩溃。请帮忙。以下是主要的代码片段。

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 with timer ticking after 1 sec, and give timer tick every 1ms

onTimerTick();
timer.schedule(mTimerTask, 1000, 1); 
}


//method for reading data from receiverQueue
protected void readDataFromReceiverBuffer() {
    //if receiverQueue is not empty, read data
    if(receiverQueue.isEmpty() == false) {
        //declaration of byte array receiverField size of receiverQueue
        receiverArray = new byte[receiverQueue.size()];
        //read bytes from queue until is empty
            for (int i = 0; receiverQueue.isEmpty() == false; i++) {
                //retrieves and removes the head of this queue, or returns null 
            receiverArray[i] = receiverQueue.poll();
            }
        //variable newData is set to true, when we read new data from queue
        newDataRead = true;
    }
}       


Thread dataReceiveThread = new Thread(new Runnable() {
    //received byte
    int receivedData;
    public void run() {
        while(!Thread.currentThread().isInterrupted() && !stopWorker) {

            try { 
                //read one byte from InputStream
                receivedData = mmInStream.read();
                //inserts received byte into this queue if it is 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 is called 
        public void run() {
            //call method for read data from queue
            readDataFromReceiverBuffer();
            timerHandler.post(new Runnable() {
                public void run() {
                    //when new data has arrived, we update UI 
                if(newDataRead == true) {
                    //convert byte array to string
                    temp = new String(receiverArray, 0, receiverArray.length);
                     myLabel1.setText(temp);
                     newDataRead = false;
                }                               
                    }
                }); 
      }};    
}    

日志猫:

W/dalvikvm(12358): threadid=11: thread exiting with uncaught exception (group=0x40ab1210)
E/AndroidRuntime(12358): FATAL EXCEPTION: Timer-0
E/AndroidRuntime(12358): java.lang.ArrayIndexOutOfBoundsException: length=21; index=21
E/AndroidRuntime(12358):    at com.example.colibri2bb.MainActivity.readDataFromReceiverBuffer(MainActivity.java:195)
E/AndroidRuntime(12358):    at com.example.colibri2bb.MainActivity.readDataFromReceiverBuffer(MainActivity.java:195)
E/AndroidRuntime(12358):    at com.example.colibri2bb.MainActivity$3.run(MainActivity.java:230)
E/AndroidRuntime(12358):    at java.util.Timer$TimerImpl.run(Timer.java:284)

日志猫:

W/dalvikvm(17344): threadid=12: thread exiting with 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): at java.util.LinkedList.removeFirst(LinkedList.java:676)
E/AndroidRuntime(17344): at 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): at java.util.Timer$TimerImpl.run(Timer.java:284)
4

4 回答 4

0

这可能是因为抛出的异常类型不是 IOException。捕获所有异常并查看 - 将 IOException 更改为 Exception。

于 2013-04-18T07:22:50.297 回答
0

你的问题在这里:java.lang.ArrayIndexOutOfBoundsException: length=21; index=21 MainActivity.readDataFromReceiverBufferreceiverArray[21] 当您的数组仅包含 21 个元素时,您尝试分配值!

于 2013-04-18T07:43:40.560 回答
0

当您从中读取数据时,是否有其他线程正在修改receiverQueue 并添加数据?尝试改用这个 for 循环。它应该确保你不会越界。

int queueSize = receiverQueue.size();
receiverArray = new byte[queueSize ];
for (int i = 0; i<queueSize; i++) {
   ...
}
于 2013-04-18T08:14:00.593 回答
0

您的receiverArray 使用队列的大小进行初始化,但是当您读取队列时队列会扩展,最终得到的数据比您预期的要多。

您必须确保您没有阅读太多,很可能是通过根据大小测试索引(额外的数据将在下一次执行时读取readDataFromReceiverBuffer

您还可以分配一个更大的数组(但测试仍然是强制性的)

您的第二个问题可能是您的任务太长,因此对队列有一些重叠和并发访问。(基本上,2个实例测试队列是否为空,都尝试从中读取,但只有一个元素)

我会使用一个带有循环的线程,循环执行的时间测量,以及延迟到下一次需要执行的睡眠,而不是计时器。这将确保对读取队列的单一访问。

于 2013-04-18T08:11:31.143 回答