主题/处理程序错误 - 指定的消息队列同步屏障令牌尚未发布 [英] Thread/Handler error - The specified message queue synchronization barrier token has not been posted

查看:233
本文介绍了主题/处理程序错误 - 指定的消息队列同步屏障令牌尚未发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误 -

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

java.lang.IllegalStateException: The specified message queue synchronization barrier token has not been posted or has already been removed.

作为一个相对较新的的Java / Android的,这是毫无疑问不是我错过了,但我做的是这样的 -

As a relative newcomer to Java/Android, there is no doubt something I have missed, but what I am doing is this -

我有使用的Exif数据根据其拍摄日期来显示照片项目,而其意图是使用了类似的模型中的每个阶段...

I have a project which uses Exif Data to display Photos according to the date they were taken, and the intention is to use a similar model on each stage...

工作线程 - > UI线程 - >自定义显示适配器。然后点击在GridView中单元格1触发下一个活动。所有照片文件的第一个活动的搜索,打造十年的名单,然后以后每次活动它过滤到月,日等。

Worker Thread -> UI Thread -> Custom Display Adapter. Then clicking on one of the "Cells" in GridView triggers the next Activity. The First Activity searches for all the Photo Files, creating a list of "Years", and then each subsequent activity filters it to months, days etc.

不过开始的第二个活动启动直入上面的错误,并且消息通过基本的线程/处理程序的建立处理。

Starting the second activity however launches straight into the above error, and the Messages are dealt with via the basic Thread/Handler set-up.

下面是传递消息给线程的类 -

Here is the class that is passing the messages to the thread -

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);*/
}

...这是code表示接收它在UI线程

... and this is the code that receives it in the UI Thread.

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");
            }
        }
    }
};

请注意,我并​​没有包括所有的code,只是我觉得是相关的部分。

Please note that I haven't included all of the code, just the parts I feel are relevant.

在previous活动管理,以同样的方式运作成功的消息,为什么没有第二个活动呢?

The previous activity managed to successfully operate messages in the same way, why not the second activity ?

据我所知,主UI线程已经有一个活套设置,因此您不必创建一个。是正在开展任何后续活动仍然如此吗?

I understand that the Main UI thread already has a looper set-up, and therefore you don't have to create one. Is that still true of any subsequent Activities that are launched ?

推荐答案

我得到了同样的问题,因为你在这里做。经过两天的斗争中,我找到了一种方法来解决它。 这很简单。只是在你的handlerMessage()更新任何UI前添加this.obtainMessage()。 这样做了以后,一切都将现在被罚款。

I got the same problem as you did here. After two days' struggle, I found a way to solve it. It is simple. Just add this.obtainMessage() in your handlerMessage() before updating any UI. After doing this, everything will be fine now.

我想这是因为我们的UI线程和后台线程之间的太快,在这种情况下Android系统无法调度消息妥善全部由自己沟通。当我们强迫的Andr​​oid这样做,问题就迎刃而解了。 我不知道,这只是一个猜测。 我希望它可以帮助你。

I guess it was because we are communicating between UI thread and background thread too quickly in which situation the Android system can not dispatch Msg properly all by itself. When we force Android to do so, problem solved. I am not sure, it's just a guess. I hope it can help you.

这篇关于主题/处理程序错误 - 指定的消息队列同步屏障令牌尚未发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆