首先,让我解释一下我的代码:当用户单击按钮时,会执行以下代码:
mMyListView.removeFooterView(mMyFooterView);
mMyListView.setAdapter(null);
//Fetch some data in an AsyncTask
异步完成后,将执行以下代码:
if (mMyList.size() > 0){
mMyListView.addFooterView(mMyFooterView);
}
MyListAdapter myListAdapter = new MyListAdapter(this, mMyList);
mMyListView.setAdapter(myListAdapter);
这就是事情出错的地方。因为在某些设备上,我在这条线上得到了 NullPointerException:
mMyListView.addFooterView(mMyFooterView);
这是堆栈跟踪:
java.lang.NullPointerException
at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:778)
at android.widget.ListView.addFooterView(ListView.java:387)
at android.widget.ListView.addFooterView(ListView.java:402)
at com.augustinianum.augustinianum.ManualCheckActivity.showScheduleChanges(ManualCheckActivity.java:84)
at com.augustinianum.augustinianum.ManualCheckActivity.access$1(ManualCheckActivity.java:81)
at com.augustinianum.augustinianum.ManualCheckActivity$CheckScheduleChange.onPostExecute(ManualCheckActivity.java:192)
at com.augustinianum.augustinianum.ManualCheckActivity$CheckScheduleChange.onPostExecute(ManualCheckActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
奇怪的是,只有当用户第二次单击获取数据的按钮时,我才会收到此异常。第一次一切顺利:显示 listView 并添加页脚。但是第二次,我在某些设备上遇到了这个异常。
有谁知道如何解决这一问题?