Java – thread / handler error – the specified message queue synchronization barrier flag has not been published
I received this error –
As a relative newcomer to Java / Android, there is no doubt that I missed something, but what I am doing is this –
I have a project that uses EXIF data to display photos according to the shooting date. The purpose is to use similar models on each stage
Worker thread – > UI thread – > custom display adapter Then click a "cell" in the GridView to trigger the next activity The first activity searches all photo files, creates a "year" list, and then filters it into months, days, etc. for each follow-up activity
However, starting the second activity will directly start the above error and process the message through the basic thread / handler settings
This is the class that passes messages to threads –
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);*/
}
... 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,tn.length);
photos.add(md);
iAdapter.notifyDataSetChanged();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("Debug","handler error occurs in UI Handler");
}
}
}
};
Please note that I don't include all the code, just what I think is relevant
The previous activity successfully manipulated the message in the same way, why not the second activity?
I know the main UI thread already has a looper setting, so you don't have to create one Is it still applicable to any follow-up activities?
Solution
This problem is solved by using the dispatcher message method of handler instead of SendMessage
