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:
Now compare above
removeFooterView(...)method with the implementation of a more recent platform: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 ifmAdapter == 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() != nullbefore trying to remove the footer view.