gpt4 book ai didi

android - 导航到另一个 Fragment 时,Fragment 中的 RecyclerView 崩溃

转载 作者:IT老高 更新时间:2023-10-28 13:45:28 24 4
gpt4 key购买 nike

我一直在关注网络上的示例来创建 RecyclerView。我唯一不同的是把 RecyclerView 放在 Fragment 中,而不是放在 MainActivity 中。 RecyclerView 很好地显示了数据。但是当我导航到另一个 fragment 时,应用程序崩溃并出现与 RecyclerView 相关的异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference

这是一个最小的重现示例:

主 Activity :

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_layout)
}
}

main_layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment class="package.RecyclerFragment"
android:id="@+id/fragment"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

RecyclerFragment:

class RecyclerFragment : Fragment() {

private val data = listOf("Moscow", "Washington")

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
setHasOptionsMenu(true)
val view = inflater.inflate(R.layout.recycler_list, container, false)
view.findViewById<RecyclerView>(R.id.list)?.apply {
adapter = RecyclerAdapter(data)
}
return view
}

override fun onCreateOptionsMenu(menu: Menu?, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.menu, menu)
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
return when (item?.itemId) {
R.id.navigate -> {
fragmentManager?.beginTransaction()?.replace(R.id.fragment, HelloFragment())?.commit()
true
}
else -> super.onOptionsItemSelected(item)
}
}
}

recycler_list:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/list"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

回收器适配器:

class RecyclerAdapter(private val data: List<String>):
RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {

inner class ViewHolder(val view: CardView): RecyclerView.ViewHolder(view)

override fun onCreateViewHolder(root: ViewGroup, viewType: Int): ViewHolder {
val listItem = LayoutInflater.from(root.context)
.inflate(R.layout.list_item, root, false) as CardView
return ViewHolder(listItem)
}

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
viewHolder.view.findViewById<TextView>(R.id.text).text = data[position]
}

override fun getItemCount() = data.size
}

列表项:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="5sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp"
android:textSize="20sp"
android:id="@+id/text"/>
</android.support.v7.widget.CardView>

菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/navigate"
android:title="Navigate"
app:showAsAction="ifRoom"/>
</menu>

HelloFragment:

class HelloFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.hello, container, false)
}
}

你好:

<?xml version="1.0" encoding="utf-8"?>
<TextView android:text="Hello"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"/>

这个实现有问题吗?如何在 Fragment 中使用 RecyclerView?

最佳答案

@Jeel 和 @Birju 向您展示了使用 Fragment 的正确方法,但我仍然留下我的答案,以防您想更深入地了解为什么您的实现不起作用。

原因:

首先,查看 main_layout:

<ConstraintLayout>

<fragment class="package.RecyclerFragment"
android:id="@+id/fragment"
... />

</ConstraintLayout>

main_layout在 MainActivity 中膨胀,<fragment>元素被简单地替换为 RecyclerFragment 布局中包含的任何内容 a.k.a recycler_list布局。

所以 main_layout实际上会变成:

<ConstraintLayout>

<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/list"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</ConstraintLayout>

如果你把这些代码放在MainActivity的onResume()中,你可以看得很清楚:

    override fun onResume() {
super.onResume()
val parent = findViewById<ConstraintLayout>(R.id.constraint)
val numChild = parent.childCount
val childView = parent.getChildAt(0)
Log.d("Parent", parent.toString())
Log.d("NumChild", numChild.toString())
Log.d("ChildView", childView.toString())
return
}

// Log
D/Parent: androidx.constraintlayout.widget.ConstraintLayout{a6e5545 V.E...... ......I. 0,0-0,0 #7f07004d app:id/constraint}
D/NumChild: 1
D/ChildView: androidx.recyclerview.widget.RecyclerView{753849a VFED..... ......I. 0,0-0,0 #7f070060 app:id/fragment}

因此,当您调用此行时:

fragmentManager?.beginTransaction()?.replace(R.id.fragment, HelloFragment())?.commit()

它实际上将 RecyclerView 作为 container View 组,并将 HelloFragment 布局中的任何内容添加到 RecyclerView

作为证据,您可以查看 FragmentManager 类中的这些行:


// mContainerId here is R.id.fragment in your layout

container = (ViewGroup) mContainer.onFindViewById(f.mContainerId);

// Now container is RecyclerView

...

f.performCreateView(f.performGetLayoutInflater(
f.mSavedFragmentState), container, f.mSavedFragmentState)

// After this line, f.mView is the view inside hello.xml

...

container.addView(f.mView);

// Add layout in hello.xml into RecyclerView

因为 RecyclerView 旨在保存从 Adapter 中的数据创建的 ViewHolders,所以即使在 RecyclerView 中添加 Hello 的 fragment View 并删除所有 ViewHolders 后,它仍然保留一个名为 childCount 的变量(在本例中为 3)来自它。

当添加新 View 时,RecyclerView 调度新布局,然后调用名为 findMinMaxChildLayoutPositions() 的函数

private void findMinMaxChildLayoutPositions(int[] into) {

final int count = mChildHelper.getChildCount();
...
for (int i = 0; i < count; ++i) {
final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i));
if (holder.shouldIgnore()) {
continue;
}

如您所见,因为所有 ViewHolders 都已被移除,holder will be null当涉及到行 if (holder.shouldIgnore()) { 时,将抛出 NPE

感谢您阅读这么长的答案!

关于android - 导航到另一个 Fragment 时,Fragment 中的 RecyclerView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56940983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com