![](/img/trans.png)
[英]Main null exception generated by listview.setadapter(adapter)
[英]Why doesn't ListView.setAdapter(null) throw a null pointer exception?
我偶然發現了這個獨特的發現。 我的代碼壞了,因為實現了LoaderManager之后,當我之前沒有異常時,在我的適配器上收到了NullPointerException
異常。
因此,我使用了調試器並逐步執行了onCreate方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
// Irrelevant code removed
mDrawerListView.setAdapter(mAccountAdapter);
return mDrawerListView;
}
不相關的代碼僅包括添加頁腳視圖和設置onClickListeners,但在此方法(或任何其他方法)中沒有一次,我是否mAccountAdapter
分配某些mAccountAdapter
。 我知道我應該做,而且我要去,但是我犯了忘記這樣做的錯誤。
當我使用調試器並在setAdapter(mAccountAdapter)
行上設置斷點時,我可以看到mAccountAdapter
為空。 但是,我可以跨過它繼續前進,而不會遇到空指針異常。 為什么允許將空值傳遞給該函數?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.