ListView SetAdapter has already been called.

mListView.setAdapter(mListAdapter);
mListView.addHeaderView(mHeaderView);

java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.

这个错误发生在Android4.3(包括4.3,即18)以下。

有两种解决办法。

  • 在setAdapter之前addHeaderView
  • 如果有特殊场景需要在setAdapter之后再addHeaderView,那么可以这样:
    View fakeHeaderView = new View(context); mListView.addHeaderView(fakeHeaderView); mListView.setAdapter(mListAdapter); mListView.removeHeaderView(fakeHeaderView);

你可能感兴趣的:(ListView SetAdapter has already been called.)