提问人:Jonas Verschueren 提问时间:7/31/2023 更新时间:7/31/2023 访问量:32
尝试向片段添加芯片时,布尔值 androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore() 上的 NullPointerException
NullPointerException on boolean androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore() when trying to add Chips to a fragment
问:
我想制作一个屏幕,您可以在其中根据芯片进行过滤。添加芯片时,出现以下错误:java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference
这些是不同的文件:
xml 片段:list_members_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="listMembersViewModel"
type="com.example.test.screens.members.ListMembersViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListMembersFragment">
<HorizontalScrollView
android:id="@+id/chips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_normal"
android:contentDescription="@string/horizontal_scrollview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.chip.ChipGroup
android:id="@+id/roles_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_normal"
app:singleSelection="true" >
</com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/member_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="@dimen/spacing_small"
android:layout_marginTop="16dp"
android:layout_marginEnd="@dimen/spacing_small"
android:layout_marginBottom="@dimen/spacing_normal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chips"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
用于存储一个芯片的 xml 布局文件roles.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Choice">
</com.google.android.material.chip.Chip>
ListMembersFragment.kt
这是发生错误的地方,行chipGroup.addView(chip)
使应用程序崩溃
class ListMembersFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding: ListMembersFragmentBinding =
DataBindingUtil.inflate(inflater, R.layout.list_members_fragment, container, false)
val viewModelFactory = ListMembersViewModelFactory()
val viewModel = ViewModelProvider(this, viewModelFactory)[ListMembersViewModel::class.java]
binding.listMembersViewModel = viewModel
binding.lifecycleOwner = this
val adapter = ListMembersAdapter( MemberListener{
memberID ->
findNavController().navigate(
ListMembersFragmentDirections.actionListMembersFragmentToMemberFragment(
memberID
))
})
val recyclerView = binding.memberList
recyclerView.adapter = adapter
viewModel.listMembers.observe(viewLifecycleOwner) { adapter.submitList(it) }
viewModel.listMembers.observe(viewLifecycleOwner, object: Observer<List<Member>> {
override fun onChanged(data: List<Member>?) {
data ?: return
//1: Make a new Chip view for each item in the list
try {
val chipGroup = binding.memberList
val inflator = LayoutInflater.from(chipGroup.context)
var children = data.map { member ->
val role = member.role
val chip = inflator.inflate(R.layout.roles, chipGroup, false) as Chip
chip.text = role.toString()
chip.tag = role.toString()
chip.setOnCheckedChangeListener{ button, isChecked ->
viewModel.onFilterChanged(button.tag as String, isChecked)
}
chip
}.distinctBy { it.tag }
//2: Remove any views already in the ChipGroup
chipGroup.removeAllViews()
//3: Add the new children to the ChipGroup
for (chip in children) {
Log.i("ListMembersFragment", chip.tag.toString())
chipGroup.addView(chip)
}
Log.i("ListMembersFragment", "ADDED CHILDREN TO CHIPGROUP VIEW")
} catch (er: Error) {
Log.e("ListMembersFragment", er.toString())
}
}
})
return binding.root
}
}
ListMembersViewModel.kt
class ListMembersViewModel : ViewModel() {
private var _filter = FilterHolder()
private val _listMembers = MutableLiveData<List<Member>>()
val listMembers: LiveData<List<Member>>
get() = _listMembers
init {
initializeLiveData()
}
private fun initializeLiveData(){
viewModelScope.launch {
_listMembers.value = MemberMock().members
}
}
fun onFilterChanged(filter: String, isChecked: Boolean) {
Log.i("ListMembersViewModel", "In method onFilterChanged")
if (this._filter.update(filter, isChecked)) {
val members = MemberMock().members
_listMembers.value = members.filter { m ->
m.role.toString() == _filter.currentValue
}
} else {
_listMembers.value = MemberMock().members
}
}
private class FilterHolder {
var currentValue: String? = null
private set
fun update(changedFilter: String, isChecked: Boolean): Boolean {
Log.i("ListMembersViewModel", "In class method update in FilterHolder")
if (isChecked) {
currentValue = changedFilter
return true
} else if (currentValue == changedFilter) {
currentValue = null
return true
}
return false
}
}
}
我真的不知道问题发生在哪里。以下是完整的例外情况:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 31991
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference
at androidx.recyclerview.widget.RecyclerView.findMinMaxChildLayoutPositions(RecyclerView.java:4485)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4215)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4010)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at androidx.coordinatorlayout.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1213)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:899)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:919)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1263)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答: 暂无答案
评论