Android:索引0无效,大小为0.在哪里?

时间:2015-09-20 10:37:16

标签: java android indexoutofboundsexception

我收到无效索引0,大小为0错误,无法确定这种情况发生的位置。我一直在运行调试器并且无法放置它。 我所知道的是我之前的代码中会发生什么以及之后会发生什么。 下面我发布了logcat,崩溃前完成的代码以及之后运行的代码。

这里是logcat:

   @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Answer a = null;
    if(position -1 >= 0 && myAnswerNotices.size() > 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 && !myAnswerNotices.isEmpty() && myAnswerNotices.contains(a))
            myAnswerNotices.remove(a);
    }

    startActivity(intent);

}

这是我之前的代码,它完成了:

  @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();

}

以下是应该开始的代码:

public class NotificationFragment extends Fragment
    implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {

private final List<Answer> myAnswerNotices = new ArrayList<>();

@Colin:

myAnswerNotices初始化为成员变量:

    @Override
    protected void onPreExecute() {
        mRefreshWrapper.setRefreshing(true);
       // Activity.pinAll();
    }

    @Override
    protected List<Answer> doInBackground(Object... params) {

        //myAnswerNotices.clear();
       // List<Activity> a_list = new ArrayList<>();
        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<Answer> list) {
        if(!myAnswerNotices.isEmpty())
            mHeaderTextView.setVisibility(View.GONE);
        else {
            mHeaderTextView.setText("No notifications");
            mHeaderTextView.setVisibility(View.VISIBLE);
        }

        mRefreshWrapper.setRefreshing(false);

    }

并填充在AsyncTask中: 公共类RefreshTask扩展了AsyncTask&gt; {

NotificationListAdapter mListAdapter = new NotificationListAdapter(getActivity(), R.layout.notification_list_item, myAnswerNotices);
    mRefreshWrapper.getListView().setAdapter(mListAdapter);
    mListAdapter.setNotifyOnChange(true);

@Varsha: 这是与我的适配器初始化有关的代码:

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

@Titus: 这是我的整个适配器类:

{{1}}

}

1 个答案:

答案 0 :(得分:9)

注意:我没有足够的声誉来评论,所以写下这个答案。

似乎适配器中的getCount()方法只是覆盖了该方法:

if(myAnswerNotices == null || myAnswerNotices.size()>0)
return myAnswerNotices.size();
else 
return 0;

我希望这会有所帮助。 感谢