2016-06-21 45 views
2

拋出我有一個ListView,我傳遞一個空適配器,然後我填充一次數據從服務器拉:NoSuchElementException異常從ListView控件

Context mContext; 
List<Recipe> menuList = new ArrayList<>(); 
private Party party; 

public MenuAdapter(Context context) { 
    mContext = context; 
    party = PartyPlannerConfig.getCurrentParty(); 
} 

public void replaceList(final @NonNull List<Recipe> menuList) { 
    this.menuList = menuList; 
    notifyDataSetChanged(); 
} 

在我getView方法,我填寫的意見,我也有關於這增加了從一個最填充適配器/刪除從一個單獨的列表項視圖之一點擊收聽:

@Override 
public View getView(int position, View convertView, final ViewGroup parent) { 
    final View view = LayoutInflater.from(mContext).inflate(R.layout.content_recipe_element, parent, false); 

    final TextView mButton = (TextView) view.findViewById(R.id.name); 
    final ImageView mImage = (ImageView) view.findViewById(R.id.image); 
    final ImageView mBtnFavourite = (ImageView) view.findViewById(R.id.btn_favourite); 

    final Recipe recipe = getItem(position); 
    Picasso.with(mContext).load(recipe.getTableImageURL()).into(mImage); 
    mButton.setText(recipe.getRecipeName()); 

    final List<Recipe> currentRecipes = new ArrayList<>(party.getMenuItems()); 
    final List<Recipe> modifiedRecipes = new ArrayList<>(party.getMenuItems()); 

    mBtnFavourite.setSelected(currentRecipes.contains(recipe)); 

    mBtnFavourite.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try { 
       if (!mBtnFavourite.isSelected()) { 
        modifiedRecipes.add(recipe); 
       } else { 
        if (modifiedRecipes.contains(recipe)) { 
         modifiedRecipes.remove(recipe); 
        } 
       } 
       party.setMenuItems(modifiedRecipes); 
       notifyDataSetChanged(); 
       new SavePartyTask().execute(party); 
      } catch (Exception ignore) { } 
     } 
    }); 

    mImage.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mContext.startActivity(RecipeDetailActivity.makeIntent(mContext, recipe)); 
     } 
    }); 

    return view; 
} 

出於某種原因,當我點擊mBtnFavourite一遍又一遍,我得到一個NoSuchElementException:

AndroidRuntime:... 
        java.util.NoSuchElementException 
                       at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576) 
                       at com.backendless.FootprintsManager$Inner.updateFootprintForObject(FootprintsManager.java:257) 
                       at com.backendless.Persistence$4.handleResponse(Persistence.java:196) 
                       at com.backendless.async.message.AsyncMessage$ResponseHandler.handle(AsyncMessage.java:64) 
                       at com.backendless.async.message.AsyncMessage.handleCallback(AsyncMessage.java:41) 
                       at com.backendless.core.AndroidCarrier$1.handleMessage(AndroidCarrier.java:37) 
                       at android.os.Handler.dispatchMessage(Handler.java:98) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

下面是我的AsyncTask代碼:

private class SavePartyTask extends AsyncTask<Party, Void, Void> { 
    @Override 
    protected Void doInBackground(Party... params) { 
     new PartyController(mContext).saveParty(params[0]); 
     return null; 
    } 
} 

,這是PartyController代碼:我是超越困惑

公共類PartyController擴展控制器{

private static PartyController instance; 

public PartyController(Context context) { 
    super(context); 
} 

public synchronized static PartyController getInstance(Context context) { 
    if(instance == null) { 
     instance = new PartyController(context); 
    } 
    return instance; 
} 

public void getPartyList() throws UserException { 
    mJobManager.addJobInBackground(new GetPartyListJob()); 
} 

public void addParty(String partyName, Occasion occasion, Moodboard inspiration) throws UserException { 
    final Party party = new Party(); 
    final Long currentTime = (System.currentTimeMillis()/1000L); 

    party.setName(partyName); 
    party.setCreated(currentTime); 
    party.setOccasion(occasion); 
    party.setMoodboard(inspiration); 

    final Calendar c = Calendar.getInstance(); 
    party.setUpdated(c.getTimeInMillis()); 

    PartyPlannerLog.v(WHOAMI, "Logged in user: " + Backendless.UserService.loggedInUser()); 
    mJobManager.addJobInBackground(new SavePartyJob(party)); 
} 

public void deleteParty(Party party) throws UserException { 
    mJobManager.addJobInBackground(new DeletePartyJob(party)); 
} 

public void saveParty(Party party) { 
    final Calendar c = Calendar.getInstance(); 
    party.setUpdated(c.getTimeInMillis()); 
    PartyPlannerConfig.setCurrentParty(party); 
    mJobManager.addJobInBackground(new SavePartyJob(party)); 
} 

} 在這個時候,一直試圖找出這一整天。 Plz幫助!

+0

請發佈您的異步任務代碼和派對類。 – Francesc

+0

@Francesc,完成 –

回答

1

我認爲你有一個併發的問題:

party.setMenuItems(modifiedRecipes); 
new SavePartyTask().execute(party); 

它放在這裏:

PartyPlannerConfig.setCurrentParty(party); 
mJobManager.addJobInBackground(new SavePartyJob(party)); 

我不能肯定什麼這兩個函數做的,但如果他們繼續爲參考列表而不是製作副本,那麼您可以在後臺線程中訪問列表,而UI線程修改按鈕單擊時的列表。

一個簡單的解決方案在這裏將讓你列表的副本在你的黨的對象,而不是保持原來的一個參考:

party.setMenuItems(modifiedRecipes); 

確保這setMenuItems使得modifiedRecipes的副本,而不是保留對原件的引用。

+0

謝謝。我認爲併發實際上是這裏的問題。 –

1

是的,它看起來像併發問題。仔細調試您的代碼,在此基礎上添加項目並將其刪除到列表中。我想,在你的代碼嘗試刪除一個沒有的元素的時候。使用記錄器跟蹤列表中的項目,並檢查是否正在調用空列表。

相關問題