![](/img/trans.png)
[英]java.lang.IllegalStateException: [TextView] must not be null
[英]java.lang.IllegalStateException: TextView must not be null (Android/Kotlin)
我的 Recycler View 有以下 ViewHolder 類,
inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val dateText = itemView.itemDateSummaryList
private val systolicVal = itemView.systolicValue
private val diastolicVal = itemView.diastolicValue
fun update(listItem: SummaryListItemModel) {
Log.i(TAG, "Update method called " + listItem.date)
dateText.text = listItem.date
systolicVal.text = listItem.sysVal.toInt().toString()
diastolicVal.text = listItem.diasVal.toInt().toString()
}
}
但是當我運行應用程序時, dateText.text = listItem.date
出現錯誤,說,
java.lang.IllegalStateException: dateText must not be null
at *****.******.*****.ui.detailview.bloodpressure.SummaryListAdapter$ItemViewHolder.update(SummaryListAdapter.kt:68)
但是listItem.date
不是空的我已經檢查了日志。
該錯誤與listItem.date
無關,該錯誤表明您嘗試設置文本的dateText
文本dateText
為 null ,
仔細檢查您使用的是正確的textview
可能性:
1)您可能使用了錯誤的textview
id
2)您可能在膨脹視圖時使用了錯誤的文件。
ctrl
+ 單擊itemDateSummaryList
並查看textview
是否來自與您擁有infate
或其他方式相同的布局文件
如果您使用片段: 1. 使用一個全局變量來保存您的布局引用
private var root: View? = null // create a global variable which will hold your layout
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
root = inflater.inflate(R.layout.your_layout_file_name, container, false) // initialize it here
return root
}
root.
) 一起使用,例如:
root?.textView.text="Hello"
它是說您的dateText
視圖本身為空,而不是值listItem.date
驗證您是否正確呈現它, itemview
具有您嘗試訪問的 ID 的TextView
我在使用 Fragment 時也發生了同樣的情況,我從onCreateView
取出我的代碼並將其放入onViewCreated
方法,問題解決了。
我的片段收到這個錯誤,我解決了這個錯誤,
錯誤代碼:
class MyFragment : Fragment(){
private var item: Item? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
item = arguments?.getParcelable(BUNLDE_ITEM)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
item?.let {
textViewTitle.text = "Text"
}
return inflater.inflate(R.layout.my_fragment, null, false)
}
companion object {
private const val BUNLDE_ITEM = "item"
fun newInstance(item: Item) = MyFragment().apply {
arguments = Bundle().apply {
putParcelable(BUNLDE_ITEM, item)
}
}
}
}
工作代碼:
class MyFragment : Fragment() {
private var item: Item? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
item = arguments?.getParcelable(BUNLDE_ITEM)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val root: View = inflater.inflate(R.layout.my_fragment, container, false)
val textViewTitle = root.findViewById<View>(R.id.textViewTitle) as TextView
item?.let {
textViewTitle.text = "text"
}
return root
}
companion object {
private const val BUNLDE_ITEM = "item"
fun newInstance(item: Item) = MyFragment().apply {
arguments = Bundle().apply {
putParcelable(BUNLDE_ITEM, item)
}
}
}
}
它也可能發生在 xml 文件中錯誤的上下文引用上,通常它出現在手動將 Activity 移動到另一個包時,Android Studio 沒有自動進行此更改:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:tools="http://schemas.android.com/tools"
....
tools:context=".view.ui.login.LoginActivity">
....
</androidx.constraintlayout.widget.ConstraintLayout>
我閱讀了所有這些答案,但沒有一個對我有用,所以這是我的答案。
找到與此類似的代碼行:
var view:View?=inflater.inflate(R.layout.fragment_profilefragement, container, false)
將光標放在fragement_ProfileFragement
,然后按CTRL鍵單擊。 檢查您是否在正確的片段XML 文件中。
我的 RecyclerView 遇到了同樣的問題。 它已經工作了幾個月。 在無數次失敗的故障排除步驟之后,我做了一個 Build > Clean Project 並解決了這個問題。
您仔細查看,因為在您的代碼的某些部分,您沒有正確引用視圖。 換句話說,您的代碼中不存在該特定視圖。
這將節省您數小時查找錯誤的時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.