Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 队列上的NoTouchElementException_Java_Android_Multithreading_Exception_Nosuchelementexception - Fatal编程技术网

Java 队列上的NoTouchElementException

Java 队列上的NoTouchElementException,java,android,multithreading,exception,nosuchelementexception,Java,Android,Multithreading,Exception,Nosuchelementexception,我在Java中的队列中遇到了一些问题。声明如下: Queue<MotionEvent> touchQueue = new LinkedList<MotionEvent>(); 我通过调用synchronized函数来使用主线程上的事件: public synchronized void UpdateTouches() { while(!touchQueue.isEmpty()) { try { MotionEvent me

我在Java中的
队列中遇到了一些问题。声明如下:

Queue<MotionEvent> touchQueue = new LinkedList<MotionEvent>();
我通过调用
synchronized
函数来使用主
线程上的事件:

public synchronized void UpdateTouches() {
    while(!touchQueue.isEmpty()) {
        try {
            MotionEvent me = touchQueue.poll();
            ProcessTouchEvent(me);
        } catch (NoSuchElementException e) {
            return;
        }
    }
}
问题是,有时轮询会引发一个
NoTouchElementException
,之后所有后续轮询调用都会引发该
异常

有人知道原因是什么吗?或者是否有任何方法可以在不再次获得
异常的情况下移除头部对象

请注意,
size()
发生异常时返回>0

谢谢, /C

编辑:

以下是调用堆栈:

FATAL EXCEPTION: GLThread 302 java.util.NoSuchElementException
    at java.util.LinkedList.removeFirstImpl(LinkedList.java:689)
    at java.util.LinkedList.remove(LinkedList.java:899)
    at com.xxxxx.GG.GGActivity.UpdateTouches(GGActivity.java:718)
    at com.xxxxx.GG.GGView$Renderer.onDrawFrame(GGView.java:414)
    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Force finishing activity
下面是生成日志的函数的最新版本:

public synchronized void UpdateTouches()
{
    //System.out.println("Got touch event" + touchQueue.size());
    while(!touchQueue.isEmpty())
    {
        try {
            MotionEvent me = touchQueue.poll();
            ProcessTouchEvent(me);
        } catch (NoSuchElementException e)
        {
            touchQueue.remove();
            return;
        }

    }
}
当您已经知道队列为空时调用remove()没有任何意义。在一个不抛出它的方法上捕获NoTouchElementException,同上。只需删除所有代码

但是您的代码不是线程安全的。调用add()的方法需要在队列上同步,调用isEmpty()和poll()的方法同上,并且应该在队列为空时在队列上等待


或者使用java.util.concurrent队列。

从LogCat中显示该异常的调用堆栈。我将删除捕获并让它崩溃以进行崩溃转储,我将在稍后回到办公桌时返回。我添加了调用堆栈以及函数的最新版本。我认为使用touchQueue.clear()可以避免崩溃;而不是touchQueue.remove()@SuleaCosmin如果队列中没有任何内容,则调用remove()是错误的,而调用clear()只是毫无意义。我知道文档中指出,poll不会抛出NoSuchElementException,但当touchQueue.size()返回正值而touchQueue.isEmpty()返回false时,它是如何抛出的,我仅从onDrawFrame调用它(我没有任何其他机制从队列中删除项目)。我将尝试同步添加的块,并查看是否有任何更改。
public synchronized void UpdateTouches()
{
    //System.out.println("Got touch event" + touchQueue.size());
    while(!touchQueue.isEmpty())
    {
        try {
            MotionEvent me = touchQueue.poll();
            ProcessTouchEvent(me);
        } catch (NoSuchElementException e)
        {
            touchQueue.remove();
            return;
        }

    }
}