空指針出現(xiàn)閃退的原因就是數(shù)據(jù)為空,所以使用數(shù)據(jù)時最好都進(jìn)行判空,特別是請求網(wǎng)絡(luò)獲取后臺數(shù)據(jù),你不知道后臺是否在維修或是返回數(shù)據(jù)格式進(jìn)行修改等問題,你沒判空都會導(dǎo)致空指針,出現(xiàn)閃退
空指針閃退問題的解決辦法:
1.從本獲取數(shù)據(jù):數(shù)據(jù)庫,文件等
2.請求網(wǎng)絡(luò)獲取后臺數(shù)據(jù)
3.從進(jìn)程間獲取其他進(jìn)程或是線程的數(shù)據(jù)
4.activity之間的數(shù)據(jù)傳遞
特別是多層級的數(shù)據(jù)格式,每一層都要進(jìn)行判斷:
如:返回的bean為 InfoDataResponse:
{
? ? ? ? id : 1,
? ? ? ? result : 200,
? ? ? ? data : {
? ? ? ? ? ? ? ? ? ? ? ? list : [?],
? ? ? ? ? ? ? ? ? ? ? ? message : ""?? ? ? ??
? ? ? ? }
? ? ? ? messages : ""
}
所以判空時,需要先判斷數(shù)據(jù)??InfoDataResponse是否null,不為null,則判斷InfoDataResponsedata.data是否null,不為null,則判斷InfoDataResponsedata.data.list是否null,不為null,則才可以使用list里面的數(shù)據(jù)文章來源:http://www.zghlxwxcb.cn/news/detail-417273.html
總之不管怎樣,進(jìn)行判空只有好處沒有壞處文章來源地址http://www.zghlxwxcb.cn/news/detail-417273.html
到了這里,關(guān)于java.lang.NullPointerException 空指針問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!