-1

I have created a home fragment in which I am using imageslider. This is the xml file

<com.denzcoskun.imageslider.ImageSlider
       android:id="@+id/slide"
       android:layout_width="match_parent"
       android:layout_height="250dp"
       app:auto_cycle="true"
       app:delay="0"
       app:period="1000"
       app:corner_radius="20"
       tools:ignore="MissingConstraints" />

and this the HomeFragment.kt file

class HomeFragment : Fragment(R.layout.fragment_home) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        val imageList= ArrayList<SlideModel>()

        imageList.add(SlideModel("https://www.pixelstalk.net/wp-content/uploads/2016/07/A-Images-HD-Screen-Download.jpg"))
        imageList.add(SlideModel("https://media.istockphoto.com/photos/message-in-a-glass-bottle-on-a-beach-at-sunset-picture-id184699888?k=20&m=184699888&s=170667a&w=0&h=SJVDAKTmhEHeEdAKqFsW2Wjx3fQ1DLzX5CPHkiclSMs="))
       

        val imageSlider= findViewById<ImageSlider>(R.id.slide)
        imageSlider.setImageList(imageList, ScaleTypes.FIT)
    }
}

I am having an error "unresolved reference findViewById " and also the app is crashing.

rs3600
  • 1
  • 1
    you can not get the view in `onCreate` in side Fragment . Please go though the Document of Fragment lifecycle once . you need view to call `findViewById` that you will inflate inside `onViewCreated`. – ADM Jul 04 '22 at 05:51

1 Answers1

0

As clarified by @ADM made some changes and it worked..

class HomeFragment : Fragment(R.layout.fragment_home) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val imageList= ArrayList<SlideModel>()

        imageList.add(SlideModel("https://www.pixelstalk.net/wp-content/uploads/2016/07/A-Images-HD-Screen-Download.jpg"))
        imageList.add(SlideModel("https://media.istockphoto.com/photos/message-in-a-glass-bottle-on-a-beach-at-sunset-picture-id184699888?k=20&m=184699888&s=170667a&w=0&h=SJVDAKTmhEHeEdAKqFsW2Wjx3fQ1DLzX5CPHkiclSMs="))

        val imageSlider= view.findViewById<ImageSlider>(R.id.slide)
        imageSlider.setImageList(imageList, ScaleTypes.FIT)
    }
}
rs3600
  • 1