Home > Software engineering >  Fragment inflate not intended xml file
Fragment inflate not intended xml file

Time:07-14

I have a problem that EditSleepPressureFactorFragment inflates other xml file fragment_sleep_record.xml. But my intention is to inflate fragment_edit_sleep_pressure.xml. A Spinner having an id "year_spinner" is in fragment_edit_sleep_pressure.xml but binding!!.yearSpinner code occure unsolved refference. And binding!!.startSleepYear code cause no error. "start_sleep_year" id is in fragment_sleep_record.xml. How I solve this problem?

EditSleepPressureFragment.kt


import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.sleeprecorder.database.SleepPressureFactor
import com.example.sleeprecorder.database.SleepRecorderDatabase
import com.example.sleeprecorder.databinding.FragmentEditSleepRecordBinding
import java.util.*

class EditSleepPressureFactorFragment: Fragment(), AdapterView.OnItemSelectedListener {
    private var binding: FragmentEditSleepRecordBinding? = null
    private var sleepPressureFactorViewModel: SleepPressureFactorViewModel? = null
    private val yearSize = 20

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentEditSleepRecordBinding.inflate(inflater)
        Log.i("EditSleepPressureFactorFragment", inflater.context.dataDir.toString())
        val sleepPressureFactorId = EditSleepPressureFactorFragmentArgs.fromBundle(requireArguments()).sleepPressureFactorId
        val application = requireNotNull(this.activity).application
        val dataSource = SleepRecorderDatabase.getInstance(application).sleepPressureFactorDao
        val viewModelFactory = SleepPressureFactorViewModelFactory(dataSource, application, sleepPressureFactorId)
        val viewModel = ViewModelProvider(this, viewModelFactory).get(SleepPressureFactorViewModel::class.java)

        binding!!.lifecycleOwner = this
        viewModel.oneSleepPressureFactor.observe(this, Observer { sleepPressureFactor ->
            Log.i("EditSleepPressureFactorFragment", sleepPressureFactor.toString())
            val sleepPressureFactorCalendar = Calendar.getInstance().apply {
                this.timeInMillis = sleepPressureFactor!!.date
            }

            val sleepPressureFactorYear = sleepPressureFactorCalendar.get(Calendar.YEAR)
            var yearDecrement = sleepPressureFactorYear
            val sleepPressureFactorMonthIndex = sleepPressureFactorCalendar.get(Calendar.MONTH)
            val yearList = (1 .. yearSize).toList().map {
                yearDecrement--
            }

            binding!!.yearSpinner //Unsolved refference

        })

        return binding!!.root
    }
    override fun onItemSelected(parent: AdapterView<*>?, view: View?, monthIndex: Int, id: Long) {
        TODO("Not yet implemented")
    }

    override fun onNothingSelected(p0: AdapterView<*>?) {
        TODO("Not yet implemented")
    }
}

fragment_sleep_pressure_factor.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    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">

    <data>
        <variable
            name="addSleepRecordViewModel"
            type="com.example.sleeprecorder.sleeprecord.SleepRecordViewModel" />

    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".sleeprecord.SleepRecordFragment">
        <TextView
            android:id="@ id/start_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:text="@string/start_sleep"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@ id/year_spinner_start_sleep"
            />

        <Spinner
            android:id="@ id/year_spinner_start_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:background="@null"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@id/start_sleep"
            app:layout_constraintEnd_toStartOf="@id/month_spinner_start_sleep"
            />
        <Spinner
            android:id="@ id/month_spinner_start_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:background="@null"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@id/year_spinner_start_sleep"
            app:layout_constraintEnd_toStartOf="@id/date_spinner_start_sleep"
            />
        <Spinner
            android:id="@ id/date_spinner_start_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:spinnerMode="dropdown"
            android:background="@null"
            app:layout_constraintStart_toEndOf="@id/month_spinner_start_sleep"
            app:layout_constraintEnd_toStartOf="@id/hour_spinner_start_sleep"
            app:layout_constraintTop_toTopOf="parent" />


        <Spinner
            android:id="@ id/hour_spinner_start_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:background="@null"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@ id/date_spinner_start_sleep"
            app:layout_constraintEnd_toStartOf="@ id/minutes_spinner_start_sleep"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="179dp"/>

        <Spinner
            android:id="@ id/minutes_spinner_start_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:background="@null"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@ id/hour_spinner_start_sleep"
            app:layout_constraintEnd_toEndOf="parent"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="191dp" />



        <TextView
            android:id="@ id/end_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:text="@string/end_sleep"
            android:background="@null"
            app:layout_constraintTop_toBottomOf="@ id/start_sleep"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@ id/year_spinner_end_sleep"
            />


        <Spinner
            android:id="@ id/year_spinner_end_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:background="@null"
            app:layout_constraintTop_toBottomOf="@id/year_spinner_start_sleep"
            app:layout_constraintStart_toEndOf="@id/end_sleep"
            app:layout_constraintEnd_toStartOf="@id/month_spinner_end_sleep"
            />

        <Spinner
            android:id="@ id/month_spinner_end_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:background="@null"
            app:layout_constraintTop_toBottomOf="@id/month_spinner_start_sleep"
            app:layout_constraintEnd_toStartOf="@id/date_spinner_end_sleep"
            app:layout_constraintStart_toEndOf="@id/year_spinner_end_sleep"
             />

        <Spinner
            android:id="@ id/date_spinner_end_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:background="@null"
            app:layout_constraintTop_toBottomOf="@id/date_spinner_start_sleep"
            app:layout_constraintStart_toEndOf="@ id/month_spinner_end_sleep"
            app:layout_constraintEnd_toStartOf="@ id/hour_spinner_end_sleep"
             />

        <Spinner
            android:id="@ id/hour_spinner_end_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:background="@null"
            app:layout_constraintTop_toBottomOf="@id/hour_spinner_start_sleep"
            app:layout_constraintStart_toEndOf="@ id/date_spinner_end_sleep"
            app:layout_constraintEnd_toStartOf="@ id/minutes_spinner_end_sleep"

             />

        <Spinner
            android:id="@ id/minutes_spinner_end_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:background="@null"

            app:layout_constraintTop_toBottomOf="@id/minutes_spinner_start_sleep"
            app:layout_constraintStart_toEndOf="@ id/hour_spinner_end_sleep"

            app:layout_constraintEnd_toEndOf="parent"
            />

        <Spinner
            android:id="@ id/kind_of_sleep"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginTop="20dp"
            android:background="@null"
            app:layout_constraintEnd_toStartOf="@ id/memo"
            app:layout_constraintHorizontal_bias="0.698"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/end_sleep" />

        <EditText
            android:id="@ id/memo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:ems="10"
            android:inputType="text"
            android:minWidth="100dp"
            android:padding="10dp"
            android:text=""
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.502"
            app:layout_constraintStart_toEndOf="@id/kind_of_sleep"
            app:layout_constraintTop_toBottomOf="@ id/date_spinner_end_sleep"
            tools:ignore="Autofill" />

        <Button
            android:id="@ id/sleep_record_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="@string/record_submit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.114"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/memo" />




    </androidx.constraintlayout.widget.ConstraintLayout>


</layout>

fragment_sleep_record.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    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">
    <data>
        <variable
            name="sleepRecordViewModel"
            type="com.example.sleeprecorder.sleeprecord.SleepRecordViewModel" />

    </data>
    <ScrollView
        android:id="@ id/sleep_record_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@ id/year_month_navigation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@ id/go_to_previous_month"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/go_to_previous_month"
                    android:textSize="22sp"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toStartOf="@id/year_month"
                    />

                <TextView
                    android:id="@ id/year_month"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textSize="22sp"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toEndOf="@id/go_to_previous_month"
                    app:layout_constraintEnd_toStartOf="@id/go_to_next_month"/>


                <TextView
                    android:id="@ id/go_to_next_month"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/go_to_next_month"
                    android:textSize="22sp"
                    app:layout_constraintStart_toEndOf="@id/year_month"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    />
                <Button
                    android:id="@ id/add_sleep_record"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/add_sleep_record"
                    app:layout_constraintTop_toBottomOf="@id/year_month"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    />
            </androidx.constraintlayout.widget.ConstraintLayout>
            <LinearLayout
                android:id="@ id/sleep_records_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</layout>

CodePudding user response:

Use file-name fragment_edit_sleep_pressure_factor.xml and binding:

binding = FragmentEditSleepPressureFactorBinding.inflate(inflater)
  • Related