gpt4 book ai didi

java - 线程/处理程序错误 - 尚未发布指定的消息队列同步屏障 token

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:03 24 4
gpt4 key购买 nike

我收到这个错误 -

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

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

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

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

然而,启动第二个 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 线程已经有一个循环程序设置,因此您不必创建一个。对于任何后续启动的 Activity ,情况是否仍然如此?

最佳答案

问题是通过使用 Handler 的 dispatchMessage 方法而不是 sendMessage 来解决的。

关于java - 线程/处理程序错误 - 尚未发布指定的消息队列同步屏障 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15868635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com