Home > front end >  Why cant my app find the view for fragment?
Why cant my app find the view for fragment?

Time:03-12

I am making an app with Kotlin, and I'm trying to place the content of my main activity (ScooterSharingActivity) inside a fragment instead. The build succeeds, but my program crashes, and I get the following error in LogCat

    Process: dk.moapd.scootersharing, PID: 8292
    java.lang.IllegalArgumentException: No view found for id 0x7f0800c6 (dk.moapd.scootersharing:id/fragment_container_view_tag) for fragment ScooterSharingFragment{92ed9ba} (def2f41e-5fd6-4855-a554-90398882b654 id=0x7f0800c6)

Does anyone know what it could be?

This is the xml file fragment_scooter_sharing.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="match_parent"
    android:orientation="vertical"
    tools:context=".ScooterSharingFragment">

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@ id/scooter_title"/>
    <!-- Button to add rides-->
    <Button
        android:id="@ id/start_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/start_button" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@ id/edit_button"
        android:layout_gravity="center"
        android:text="@string/edit_button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@ id/list_button"
        android:layout_gravity="center"
        android:text="@string/list_button"/>
    <ListView
        android:id="@ id/rides_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

and the ScooterSharingFragment.kt class

package dk.moapd.scootersharing

import ...

private const val TAG = "ScooterSharingFragment"
/**
 * A simple [Fragment] subclass.
 * Use the [ScooterSharingFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class ScooterSharingFragment : Fragment() {

        //View binding for ScooterSharingActivity
        private lateinit var binding : FragmentScooterSharingBinding

        private lateinit var adapter : ScooterArrayAdapter
        private lateinit var titleField : EditText
        private lateinit var startButton : Button
        private lateinit var editButton : Button
        private lateinit var listButton : Button
        private lateinit var scooterList : ListView

        //  A set of static attributes used in this activity class.
        companion object {
            lateinit var ridesDB : RidesDB
            private lateinit var adapter : ScooterArrayAdapter
        }

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            val view = inflater.inflate(R.layout.fragment_scooter_sharing, container, false)
            titleField = view.findViewById(R.id.scooter_title) as EditText
            startButton = view.findViewById(R.id.start_button) as Button
            editButton = view.findViewById(R.id.edit_button) as Button
            listButton = view.findViewById(R.id.list_button) as Button
            scooterList = view.findViewById(R.id.rides_list_view) as ListView


            //Singleton to share an object between activites
            ScooterSharingActivity.ridesDB = RidesDB.get(requireContext())
            val rides = ScooterSharingActivity.ridesDB.getScooters()

            ScooterSharingFragment.adapter = ScooterArrayAdapter(requireContext(), R.layout.list_rides, rides)

            with(binding){
                //Start button
                startButton.setOnClickListener{
                    //Start the application
                    Log.d(TAG, "StartRide called")
                    val intent = Intent(requireContext(), StartRideActivity::class.java) //Change baseContext to requireContext later
                    startActivity(intent)
                }
                //Edit button
                editButton.setOnClickListener{
                    //Edit ride
                    Log.d(TAG, "EditRide called")
                    val intent = Intent(requireContext(), EditRideActivity::class.java)
                    startActivity(intent)
                }
                //List button
                listButton.setOnClickListener{
                    ridesListView.adapter = ScooterSharingFragment.adapter
                }
            }

            return view
            }
        }

CodePudding user response:

Try to set your fragment like this:

class MyFragment : Fragment() {
    private var _binding: FragmentMyBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentMyBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        // Access your views in xml like this:
        binding.editButton.setOnClickListener {
            // Your code
        }

    }

}

Also make sure you add this line in build.gradle for View Binding:

buildFeatures {
    viewBinding = true
}

CodePudding user response:

//View binding for ScooterSharingActivity private lateinit var binding : FragmentScooterSharingBinding

use viewbinding then use this line otherwise ignore. next where your [ScooterSharingFragment.newInstance] code , its not there inside companion object. and when you create attach fragment in activity then check you are correctly observed or not.

  • Related