私はこれを言ったエラーがあります:
プロセス:com.example.yudha.kotlinauth、PID:16435 Java.lang.IllegalStateException:com.example.yudha.kotlinauth.fragments.VideoFragment.onCreateView(VideoFragment.kt:30)でvideo_recyclerviewをnullにすることはできません
RecyclerViewアダプター:
class MainAdapter : RecyclerView.Adapter<CustomViewHolder>(){
val videoTitles = listOf("First title", "Second", "3rd", "Moore Title")
//number of item
override fun getItemCount(): Int {
return videoTitles.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
//create view
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.video_row, parent, false)
return CustomViewHolder(cellForRow)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val videoTitle = videoTitles.get(position)
holder?.view?.video_title.text= videoTitle
}
}
class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view){
}
フラグメントアクティビティ:
class VideoFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_video, container, false)
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
return rootView
}
}
myFragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical"
xmlns:app="http://schemas.Android.com/apk/res-auto"
tools:context=".fragments.VideoFragment">
<Android.support.v7.widget.RecyclerView
Android:id="@+id/video_recyclerview"
Android:layout_width="match_parent"
Android:layout_height="wrap_content" />
</LinearLayout>
Recycler view(videorow.xml):
<?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"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="wrap_content">
<ImageView
Android:id="@+id/imageView2"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginEnd="8dp"
Android:layout_marginStart="8dp"
Android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
Android:id="@+id/video_title"
Android:layout_width="0dp"
Android:layout_height="wrap_content"
Android:layout_marginEnd="8dp"
Android:layout_marginStart="8dp"
Android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
</Android.support.constraint.ConstraintLayout>
あなたがすることを忘れました:
video_recyclerview = rootView.findViewById(R.id.id_of_video_recyclerview) as RecyclerView
したがって、コードは次のようになります:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_video, container, false)
video_recyclerview = rootView.findViewById(R.id.id_of_video_recyclerview) as RecyclerView // Add this
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
return rootView
}
私はこの問題を解決しなければなりません
単にvideo_recyclerview
からrootView.video_recyclerview
フラグメントでは、使用するビューがどこにあるかを最初に知る必要があるためです。
recyclerviewを初期化するのを忘れました。
video_recyclerview = rootView.findViewById(R.id.video_recyclerview) as RecyclerView
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
これをapp.grableファイルに追加すると、コンポーネントのIDに直接アクセスできます
apply plugin: 'kotlin-Android-extensions'
次に、これをフラグメントにインポートします
import kotlinx.Android.synthetic.main.fragment_video.*
今、すべてが魅力のように動作します
以下でコードを更新します。
RecyclerViewアダプター:
class MainAdapter : RecyclerView.Adapter<CustomViewHolder>(){
val videoTitles = listOf("First title", "Second", "3rd", "Moore Title")
//number of item
override fun getItemCount(): Int {
return videoTitles.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
//create view
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.video_row, parent, false)
return CustomViewHolder(cellForRow)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val videoTitle = videoTitles.get(position)
holder?.view?.video_title.text= videoTitle
}
}
class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view){
val video_title= view.video_title
}
フラグメントアクティビティ:
class VideoFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_video, container, false)
video_recyclerview = findViewById(R.id.video_recyclerview) as RecyclerView
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
return rootView
}
}