title: Android: Invalid index 0, size is 0. WHERE?
body: <p>I'm getting an invalid index 0, size is 0 error and can't figure out where this is happening. I've been running the debugger and can't place it.
  All I know is what happens in my code before and what would happen after.
  Below I've posted the logcat, the code that finishes before the crash, and what would run after.</p>
  
  <p>Here's the logcat:</p>
  
  <pre><code>  java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
          at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
          at java.util.ArrayList.get(ArrayList.java:308)
          at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
          at android.widget.ListView.dispatchDraw(ListView.java:3307)
          at android.view.View.draw(View.java:15174)
          at android.widget.AbsListView.draw(AbsListView.java:4318)
          at android.view.View.updateDisplayListIfDirty(View.java:14096)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.View.draw(View.java:14895)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
          at android.view.View.draw(View.java:15174)
          at android.view.View.updateDisplayListIfDirty(View.java:14096)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.View.draw(View.java:14895)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
          at android.view.View.updateDisplayListIfDirty(View.java:14091)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.View.draw(View.java:14895)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
          at android.view.View.updateDisplayListIfDirty(View.java:14091)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.View.draw(View.java:14895)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
          at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1089)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
          at android.view.View.updateDisplayListIfDirty(View.java:14091)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.View.draw(View.java:14895)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
          at android.view.View.updateDisplayListIfDirty(View.java:14091)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.View.draw(View.java:14895)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
          at android.view.View.updateDisplayListIfDirty(View.java:14091)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.View.draw(View.java:14895)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
          at android.view.View.updateDisplayListIfDirty(View.java:14091)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.View.draw(View.java:14895)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
          at android.view.View.updateDisplayListIfDirty(View.java:14091)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.View.draw(View.java:14895)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
          at android.view.View.updateDisplayListIfDirty(View.java:14091)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.View.draw(View.java:14895)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
          at android.view.View.draw(View.java:15174)
          at android.widget.FrameLayout.draw(FrameLayout.java:592)
          at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2695)
          at android.view.View.updateDisplayListIfDirty(View.java:14096)
          at android.view.View.getDisplayList(View.java:14119)
          at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:266)
          at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:272)
          at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:311)
          at android.view.ViewRootImpl.draw(ViewRootImpl.java:2531)
          at android.view.
  </code></pre>
  
  <p>Here's my code right before, which it finishes:</p>
  
  <pre><code>   @Override
  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
      Answer a = null;
      if(position -1 &gt;= 0 &amp;&amp; myAnswerNotices.size() &gt; position -1){
          a = myAnswerNotices.get(position-1);
          QuestionFragment.clicked_Question = a.getQuestion();
      }
  
      // Go to question view
      Intent intent;
  
      if(QuestionFragment.clicked_Question.getType().equals( "poll"))
          intent = new Intent (getActivity(), PollViewActivity.class);
      else
          intent = new Intent (getActivity(), QuestionViewActivity.class);
  
      if(a != null) {
          if (preferences.contains("notice_" + a.getObjectId())) {
              editor.remove("notice_" + a.getObjectId());
              editor.commit();
          }
  
          if (myAnswerNotices != null &amp;&amp; !myAnswerNotices.isEmpty() &amp;&amp; myAnswerNotices.contains(a))
              myAnswerNotices.remove(a);
      }
  
      startActivity(intent);
  
  }
  </code></pre>
  
  <p>Here's the code that should start after:</p>
  
  <pre><code>  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
      setContentView(R.layout.activity_question_view);
  
      Toolbar toolbar = (Toolbar) findViewById(R.id.question_view_toolbar);
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setHomeButtonEnabled(true);
  
      share = (ImageButton) findViewById(R.id.questionView_share_image_button);
      emailButton = (Button) findViewById(R.id.questionView_email_button);
      title = (TextView) findViewById(R.id.questionView_title_text_view);
      username = (TextView) findViewById(R.id.questionView_askedBy_text_view);
      createdAt = (TextView) findViewById(R.id.questionView_createdAt_text_view);
      followingTextView = (TextView) findViewById(R.id.questionView_follow_textview);
  
      setupQuestion();
      setListeners();
      setupList();
  
  }
  </code></pre>
  
  <p>@Colin:</p>
  
  <p>myAnswerNotices is initialized as a member variable:</p>
  
  <pre><code>public class NotificationFragment extends Fragment
      implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
  
  private final List&lt;Answer&gt; myAnswerNotices = new ArrayList&lt;&gt;();
  </code></pre>
  
  <p>and populated in an AsyncTask:
  public class RefreshTask extends AsyncTask> {</p>
  
  <pre><code>    @Override
      protected void onPreExecute() {
          mRefreshWrapper.setRefreshing(true);
         // Activity.pinAll();
      }
  
      @Override
      protected List&lt;Answer&gt; doInBackground(Object... params) {
  
          //myAnswerNotices.clear();
         // List&lt;Activity&gt; a_list = new ArrayList&lt;&gt;();
          try {
              activityNotifications.addAll(Activity.getQuery()
                      .whereEqualTo(Constants.kQollegeActivityTypeKey, Constants.kQollegeActivityTypeAnswerPosted)
                      .whereEqualTo(Constants.kQollegeActivityToUserKey, ParseUser.getCurrentUser())
                      .whereGreaterThan(Constants.kQollegeQuestionCreatedAtKey, checked)
                      .orderByDescending(Constants.kQollegeQuestionCreatedAtKey)
                      .include(Constants.kQollegeActivityAnswerKey)
                      .find());
          } catch (ParseException e) {
              e.printStackTrace();
          }
          checked = Calendar.getInstance().getTime();
          editor.putLong("notification_checked",checked.getTime());
          editor.commit();
  
  
          for(String s : preferences.getAll().keySet()){
              if(s.contains("notice_")){
                  String a_id = preferences.getString(s, null);
                  if(a_id != null){
                      try {
                          myAnswerNotices.add(Answer.getQuery().get(a_id));
                      } catch (ParseException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }
  
          for(Activity a : activityNotifications){
              myAnswerNotices.add(a.getAnswer());
          }
  
          return myAnswerNotices;
  
      }
  
      @Override
      protected void onPostExecute(List&lt;Answer&gt; list) {
          if(!myAnswerNotices.isEmpty())
              mHeaderTextView.setVisibility(View.GONE);
          else {
              mHeaderTextView.setText("No notifications");
              mHeaderTextView.setVisibility(View.VISIBLE);
          }
  
          mRefreshWrapper.setRefreshing(false);
  
      }
  </code></pre>
  
  <p>@Varsha:
  Here is the code relating to my adapter initialization:</p>
  
  <pre><code>NotificationListAdapter mListAdapter = new NotificationListAdapter(getActivity(), R.layout.notification_list_item, myAnswerNotices);
      mRefreshWrapper.getListView().setAdapter(mListAdapter);
      mListAdapter.setNotifyOnChange(true);
  </code></pre>
  
  <p>@Titus:
  Here's my entire adapter class:</p>
  
  <pre><code>class NotificationListAdapter extends ArrayAdapter {
  
  private final int mResourceId;
  private final LayoutInflater inflater;
  private final Context context;
  private NotificationListItemHolder notificationView;
  private Answer answer;
  
  public NotificationListAdapter(Context ctx, int resourceId, List objects){
      super(ctx,resourceId,objects);
      mResourceId = resourceId;
      inflater = LayoutInflater.from(ctx);
      context = ctx;
  }
  
  @Override
  public View getView (int position, View convertView, ViewGroup parent) {
      answer = (Answer) getItem(position);
  
      // Get or create cached eventView
      if(convertView == null){
          convertView =  inflater.inflate(mResourceId, null);
          notificationView = new NotificationListItemHolder(convertView);
          convertView.setTag(notificationView);
      }
      else {
          notificationView = (NotificationListItemHolder) convertView.getTag();
      }
      TextView name = notificationView.getPostedByTextView();
      name.setTag(answer);
      // Fill Data
      fillData();
  
      name.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              Intent intent = new Intent(context, ProfileActivity.class);
              Answer a = (Answer) v.getTag();
              ParseUser u = a.getPostedBy();
  
              intent.putExtra("User", u.getObjectId());
              context.startActivity(intent);
          }
      });
  
  
  
      return convertView;
  }
  
  private void fillData(){
      try {
          Question q = answer.getQuestion().fetch();
          notificationView.getTitleTextView().setText(q.getTitle());
      } catch (ParseException e) {
          e.printStackTrace();
      }
      notificationView.getPostedByTextView().setText(answer.getPostedByText());
      notificationView.getCreatedAtTextView().setText(answer.getCreatedAtText());
  }
  </code></pre>
  
  <p>}</p>