【发布时间】:2023-03-12 11:49:01
【问题描述】:
我在调用 ListView 的 setAdapter() 后尝试运行 ListView.getChildAt() 方法,但它给了我 NullPointerException。似乎设置适配器不会导致创建子视图。正如下面的方法所说,我正在尝试获取子视图,以便可以更改其背景颜色。我该如何解决这个问题?
private void showAnswers(int questionLocation)
{
List<Answer> answers = getAnswersByQuestionLocation(questionLocation);
ArrayAdapter<String> answerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1);
for (int i = 0; i < answers.size(); i++)
{
answerAdapter.add(mOptionLetters[i] +". "+ answers.get(i).getAnswerText());
}
mAnswerList.setAdapter(answerAdapter);
if (mAnswerLocationByQuestionLocation.indexOfKey(questionLocation) > -1)
{
Log.v("Child Count",String.valueOf(mAnswerList.getChildCount()));
//mAnswerList.getChildAt(
// mAnswerLocationByQuestionLocation.get(questionLocation))
// .setSelected(true);
}
}
【问题讨论】:
标签: java android android-listview nullpointerexception