【问题标题】:Thread/Handler error - The specified message queue synchronization barrier token has not been posted线程/处理程序错误 - 指定的消息队列同步屏障令牌尚未发布
【发布时间】:2013-04-07 22:31:44
【问题描述】:

我收到此错误 -

java.lang.IllegalStateException: 指定的消息队列同步屏障令牌尚未发布或已被删除。

作为 Java/Android 的新手,毫无疑问我错过了一些东西,但我正在做的是 -

我有一个项目,它使用 Exif 数据根据拍摄日期显示照片,目的是在每个阶段使用类似的模型...

工作线程 -> UI 线程 -> 自定义显示适配器。然后单击 GridView 中的一个“单元格”会触发下一个 Activity。第一个活动搜索所有照片文件,创建一个“年”列表,然后每个后续活动将其过滤为月、日等。

然而,启动第二个活动会直接进入上述错误,并且消息是通过基本的线程/处理程序设置处理的。

这是将消息传递给线程的类 -

public class MonthSort {
Handler handler;
int imageWidth;
List<PhotoData> photoList;
public MonthSort(Handler handler2, int width, List<PhotoData> pList) {
    photoList = new ArrayList<PhotoData>();
    photoList = pList;
    imageWidth = width;
    handler = handler2;
}

public void sortFiles()
{
    int month, photoCount;
    File fileName = new File("");
    Message msg = handler.obtainMessage();
    //Message msg = Message.obtain();
    //Bundle bundle = new Bundle();
    try {
        for (int i = 0; i < 12; i++) {
            month = i + 1;
            photoCount = 0;
            for (PhotoData pd : photoList) {
                if(month == pd.month)
                {
                    if(photoCount == 0)
                        fileName = pd.fileName;
                    photoCount++;
                }
            }
            if(photoCount != 0)
            {

                Bundle bundle = new Bundle();
                bundle.putString("filename", fileName.toString());
                bundle.putInt("month", month);
                bundle.putInt("count", photoCount);
                byte[] thumbNail = getThumbnail(fileName, imageWidth);
                bundle.putByteArray("thumbnail", thumbNail);


                msg.setData(bundle);
                handler.sendMessage(msg);

            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("Debug", "handler error occurs in monthSort class");
    }
    /*Bundle bundle = new Bundle();
    bundle.putBoolean("end", true);
    msg.setData(bundle);
    handler.sendMessage(msg);*/
}

...这是在 UI 线程中接收它的代码。

public class MonthActivity extends Activity {
List<PhotoData> photoList;
static List<MonthData> photos;
int imageWidth;
GridView photoGrid;
static ImageAdapter2 iAdapter;
int year;
Thread monthSortThread;

Handler handler2 = new Handler() {
    @Override
    public void handleMessage(Message msg) 
    {
        Bundle bundle = msg.getData();  // Get the message sent to the Handler.
        boolean ended = bundle.getBoolean("end");
        if(ended)
        {
            //Toast.makeText(getBaseContext(), "FINISHED !!!", Toast.LENGTH_LONG).show();
        } else
        {
            try {
                MonthData md = new MonthData();
                md.monthValue = bundle.getInt("month");
                md.monthString = getMonthString(md.monthValue);
                md.count = bundle.getInt("count");
                byte[] tn = bundle.getByteArray("thumbnail");
                md.thumbnail =  BitmapFactory.decodeByteArray(tn, 0, tn.length);
                photos.add(md);
                iAdapter.notifyDataSetChanged();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("Debug", "handler error occurs in UI Handler");
            }
        }
    }
};

请注意,我并未包含所有代码,仅包含我认为相关的部分。

之前的activity成功地以同样的方式操作消息,为什么不是第二个activity?

我了解主 UI 线程已经设置了一个活套,因此您不必创建一个。任何后续启动的活动仍然如此吗?

【问题讨论】:

    标签: java android multithreading handler


    【解决方案1】:

    使用 Handler 的 dispatchMessage 方法解决了问题,而不是 sendMessage。

    【讨论】:

      【解决方案2】:

      我遇到了和你在这里一样的问题。经过两天的挣扎,我找到了解决它的方法。 很简单。只需在更新任何 UI 之前在 handlerMessage() 中添加 this.obtainMessage() 即可。 完成此操作后,现在一切都会好起来的。

      我猜是因为我们在 UI 线程和后台线程之间的通信太快了,在这种情况下,Android 系统无法完全自行正确调度 Msg。当我们强制 Android 这样做时,问题就解决了。 我不确定,这只是一个猜测。 希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-22
        • 1970-01-01
        • 1970-01-01
        • 2019-01-15
        • 2011-09-27
        • 1970-01-01
        • 1970-01-01
        • 2018-12-05
        相关资源
        最近更新 更多