I am getting crash reports on
android.widget.ListView lv; lv.removeFooterView(v)
The error is null pointer exception. I check that listView itself is not null. What causes this? Is it necessary to make sure the view to be removed is not null? Is that enough or do I first need to also check that the footer view actually has been added?
java.lang.NullPointerException
at android.widget.ListView.removeFooterView(ListView.java:374)
It seems to me this method should be robust enough not to crash! Why does it not just return false if it cannot remove the view?
PS. I would like to know if anyone else has seen this?
Unfortunately you don't mention what Android version the error reports are coming from. However, looking at the source code, Android 2.1-update1 seems like a good candidate.
I'll just copy in the whole method to make things clear:
public boolean removeFooterView(View v) {
if (mFooterViewInfos.size() > 0) {
boolean result = false;
if (((HeaderViewListAdapter) mAdapter).removeFooter(v)) { // <- line 274
mDataSetObserver.onChanged();
result = true;
}
removeFixedViewInfo(v, mFooterViewInfos);
return result;
}
return false;
}
Now compare above removeFooterView(...)
method with the implementation of a more recent platform:
public boolean removeFooterView(View v) {
if (mFooterViewInfos.size() > 0) {
boolean result = false;
if (mAdapter != null && ((HeaderViewListAdapter) mAdapter).removeFooter(v)) {
if (mDataSetObserver != null) {
mDataSetObserver.onChanged();
}
result = true;
}
removeFixedViewInfo(v, mFooterViewInfos);
return result;
}
return false;
}
As you can see, the've added in a couple of extra checks for certain members not being null
. That would suggest that the first method will indeed fail on line 274 if mAdapter == null
, whereas that wouldn't cause a crash with the newer implementation.
To work around it, all you probably need to do is add something like lv.getAdapter() != null
before trying to remove the footer view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With