Is it recommended to check view for null with every findViewById call?

后端 未结 2 827
青春惊慌失措
青春惊慌失措 2021-02-14 13:18

When inflating an element with findViewById, Android Studio always warns me that my inflated view may return null

View v = inflater.inflate(R.layou         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-14 14:16

    I think you should never do that.

    If you do that you are masking a logic error in your program, in case you are passing a wrong id to your findView.

    If you are passing a correct id but for any reason inflater returns null that's Android problem (that will never actually happen) and you also should do nothing.

    I think this warning does not come from Android Lint.

提交回复
热议问题