NullPointerException空指針錯(cuò)誤:檢查變量是否進(jìn)行了初始化
形式一:錯(cuò)誤代碼:
footer.findViewById(R.id.load_layout).setVisibility(View.GONE);
解決方案:
private LinearLayout bottom;
bottom = footer.findViewById(R.id.load_layout);
//找到底部布局的控件,首先需要隱藏這個(gè)底部布局
bottom.setVisibility(View.GONE);
總結(jié):如果報(bào)了NullPointerException空指針錯(cuò)誤纺讲,可以將變量進(jìn)行全局初始化砾淌,然后再進(jìn)行使用
形式二:錯(cuò)誤代碼
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.mvp_demo.IPresenter.search()' on a null object reference
定義了一個(gè)IPresenter對象纫塌,但是沒有為其賦值
private IPresenter mPresenter;
解決方案:
mPresenter = new Presenter(this); //其中Presenter繼承了IPresenter
總結(jié):定義了一個(gè)對象的時(shí)候拼坎,如果因?yàn)檫@個(gè)對象而報(bào)空指針錯(cuò)誤谐丢,那么就是該對象沒有初始化(沒有為該對象進(jìn)行賦值)