Reputation: 644
I am using RecyclerView
in Fragment ImagesliderFragment
First it gives an expection of "recyclerview must not be null" so on stackover flow I got an answer that you should intialized your recyclerview by using following line
frag_rv = view.findViewById(R.id.frag_rv) as RecyclerView
But problem is that it gives me error on view. that only safe or non null asserted calls are allowed
also my RecyclerView is not resolving can anyone explain me this what is the problem ?
ImagesliderFragment
class ImagesliderFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
inflater.inflate(R.layout.fragment_imageslider, container, false)
frag_rv = view.findViewById(R.id.frag_rv) as RecyclerView
frag_rv.layoutManager = LinearLayoutManager(context, LinearLayout.HORIZONTAL, false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
}
Upvotes: 0
Views: 73
Reputation: 3930
Before you use view.findViewById
you have to initialize it, also use val
before frag_rv
like the following.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// here you have to initialize your view
View view = inflater.inflate(R.layout.fragment_imageslider, container, false)
val frag_rv = view.findViewById(R.id.frag_rv) as RecyclerView
frag_rv.layoutManager = LinearLayoutManager(context, LinearLayout.HORIZONTAL, false)
return view
}
Upvotes: 0
Reputation: 69671
Try this
class ImagesliderFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val myView = inflater.inflate(R.layout.fragment_imageslider, container, false)
val frag_rv = myView.findViewById(R.id.frag_rv) as RecyclerView
frag_rv.layoutManager = LinearLayoutManager(context, LinearLayout.HORIZONTAL, false)
return myView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
}
findViewById()
SAMPLE CODE
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
// import here your layout file like this
import kotlinx.android.synthetic.main.fragment_imageslider.view.*
class ImagesliderFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val myView = inflater.inflate(R.layout.fragment_imageslider, container, false)
myView.frag_rv.layoutManager = LinearLayoutManager(context, LinearLayout.HORIZONTAL, false)
return myView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
}
}
Upvotes: 2
Reputation: 1512
You should only use the onCreateView method to inflate the view and bind the iew in the onViewCreated method
class ImagesliderFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?) = inflater.inflate(R.layout.fragment_imageslider, container, false)!!
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val frag_rv = view.findViewById<RecyclerView>(R.id.frag_rv) frag_rv.layoutManager = LinearLayoutManager(context, LinearLayout.HORIZONTAL,
false)
}
}
Upvotes: 0
Reputation: 2233
You could change your onCreateView method to:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
) =
inflater.inflate(R.layout.fragment_imageslider, container, false).apply {
frag_rv = findViewById(R.id.frag_rv) as RecyclerView //val frag_rv if it's not declared elsewhere
frag_rv.layoutManager = LinearLayoutManager(context, LinearLayout.HORIZONTAL, false)
}
This way the view is inflated(via inflater.inflate) and you are using it to find the RecyclerView.
Upvotes: 0