sto ottenendo questo errore -Discussione errore/Handler - La coda di messaggi specificato barriera di sincronizzazione pedina non è stata pubblicata
java.lang.IllegalStateException: La coda di messaggi specificato barriera di sincronizzazione del token non è stato pubblicato o ha già stato rimosso.
Come relativamente nuovo per Java/Android, non v'è alcun dubbio qualcosa che ho perso, ma quello che sto facendo è questo -
Ho un progetto che utilizza Exif dati per visualizzare foto in base alla data sono stati presi, e l'intenzione è quella di utilizzare un modello simile su ogni fase ...
Thread lavoratore -> Discussione UI -> Adattatore display personalizzato. Quindi facendo clic su una delle "celle" in GridView si attiva la prossima attività. La prima attività cerca tutti i file di foto, creando un elenco di "anni", quindi ogni attività successiva la filtra in mesi, giorni, ecc.
L'avvio della seconda attività, tuttavia, viene avviato direttamente nell'errore sopra riportato e i messaggi vengono gestiti tramite il set di base Thread/Handler.
Qui è la classe che sta passando il messaggio al 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);*/
}
... e questo è il codice che lo riceve nel thread 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");
}
}
}
};
Si prega di notare che non ho incluso tutto il codice, solo le parti che ritengo rilevanti.
L'attività precedente è riuscita a gestire correttamente i messaggi nello stesso modo, perché non la seconda attività?
Capisco che il thread dell'interfaccia utente principale abbia già un allestimento looper, pertanto non è necessario crearne uno. È ancora vero per le attività successive che vengono lanciate?