2013-04-07 23 views
5

我得到這個錯誤 -線程/處理器錯誤 - 指定的消息隊列同步障礙令牌尚未發佈

java.lang.IllegalStateException:指定的消息隊列同步障礙令牌尚未發佈或有已被刪除。

作爲一個相對較新的的Java/Android的,這是毫無疑問不是我錯過了,但我做的是這樣的 -

我有一個使用EXIF數據根據顯示照片項目他們的拍攝日期,並打算在每個舞臺上使用類似的模型...

工作線程 - > UI線程 - >自定義顯示適配器。然後點擊GridView中的一個「單元格」觸發下一個活動。第一個活動搜索所有照片文件,創建一個「年」列表,然後每個後續活動將其過濾到幾個月,幾天等。

然而,啓動第二個活動直接進入上述錯誤,通過基本的Thread/Handler設置來處理。

這裏是傳遞消息給線程的類 -

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

請注意,我沒有包括所有的代碼,只是我覺得有關的部分。

以前的活動設法成功地以相同的方式操作消息,爲什麼不是第二個活動?

據我所知,主用戶界面線程已經有一個活套設置,因此您不必創建一個。對於任何後續的活動是否仍然如此?

回答

7

問題是通過使用Handler的dispatchMessage方法而不是sendMessage來解決的。

6

我遇到了同樣的問題,因爲你在這裏。經過兩天的鬥爭,我找到了解決辦法。 很簡單。在更新任何UI之前,只需在您的handlerMessage()中添加this.obtainMessage()即可。 做完這些之後,現在一切都會好的。

我想這是因爲我們在UI線程和後臺線程之間進行通信太快,在這種情況下,Android系統無法自行正確調度消息。當我們強制Android這樣做時,問題就解決了。 我不確定,這只是一個猜測。 我希望它能幫助你。