我正试图制作一个RecyclerView,我用Groupie Library制作了一个recyclerAdapter,对我来说更容易并且工作得很好。所以我得到了这个错误: “ recyclerview_list不能为空”。
我的文件是:
RecyclerView:recycler_list:
<?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_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
片段:ParksFragment.kt
class ParksFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_parks, container, false)
recyclerview_list.layoutManager = LinearLayoutManager(context)
val adapter = GroupAdapter<ViewHolder>()
adapter.add(ParkListFragment())
adapter.add(ParkListFragment())
adapter.add(ParkListFragment())
recyclerview_list.adapter = adapter
}
}
class ParkListFragment:Item<ViewHolder>(){
override fun bind(viewHolder: ViewHolder, position: Int) {
Log.d("Myfragment", "Recyclerview is working")
}
override fun getLayout(): Int {
return R.layout.fragment_parks
}
}
fragment_parks:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".tourism.ParksFragment">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Park Name"
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/textview_parks_fragment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.0"/>
<TextView
android:text="Place Direction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textview_parks_fragment"
app:layout_constraintEnd_toEndOf="@+id/textview_parks_fragment"
app:layout_constraintStart_toStartOf="@+id/textview_parks_fragment"/>
</android.support.constraint.ConstraintLayout>
我尝试了一些在这里找到的解决方案,但无法弄清楚该怎么做。
答案 0 :(得分:0)
在您的onCreateView
方法中,您有一个return语句。
return inflater.inflate(R.layout.fragment_parks, container, false)
在return
语句之后编写的任何代码均无法访问。
因此以下代码将永远不会执行:
recyclerview_list.layoutManager = LinearLayoutManager(context)
val adapter = GroupAdapter<ViewHolder>()
adapter.add(ParkListFragment())
adapter.add(ParkListFragment())
adapter.add(ParkListFragment())
recyclerview_list.adapter = adapter