Adam Lee
Adam Lee

Reputation: 586

Android: Retrieved view in Fragment in onViewCreated is null

I am trying to retrieve a View of a fragment in the overriden onViewCreated function. Below is my code for the onCreateView and onViewCreated methods:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val root = inflater.inflate(R.layout.alarms, container, false)

    // UI initialization: Set options of the drop-down menu
    // Set the contents of the drop-down menu (Spinner)
    val spinnerArray =
        arrayOf("Followed Parties", "All Events", "Your Alarms", "Events You Haven't Set an Alarm For")

    val alarmOptions = root.findViewById<Spinner>(R.id.alarm_viewing_options)
    val adapter = ArrayAdapter(
        requireContext(),
        android.R.layout.simple_spinner_item,
        spinnerArray
    )
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    alarmOptions.adapter = adapter


    return root
}

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

    println("Alarm List: " + activity!!.findViewById(R.id.alarmList) as LinearLayout?)
}

In the second to last line of code, I attempt to retrieve one of the views (R.id.alarmList) in the fragment's layout (R.layout.alarms). However, when I print out its value, it is null, indicating that the View was not found. This seems strange, considering that onViewCreated is called after onCreateView (in which I inflate the layout R.layout.alarms) and that the View R.id.alarmList is clearly present in the R.layout.alarms XML (alarms.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.main.EventsFragment">

    <Spinner
        android:id="@+id/alarm_viewing_options"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="8dp"
        android:paddingLeft="7dp"
        android:paddingTop="7dp"
        android:paddingRight="7dp"
        android:paddingBottom="7dp"
        android:textSize="25sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/alarmList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/pagination_menu" />

</LinearLayout>

Upvotes: 0

Views: 408

Answers (2)

Zain
Zain

Reputation: 40840

This is normally because you use findViewById of the activity; so it points to the activity layout not the fragment layout .. instead you can either use the view parameter of the onViewCreated() or requireView()

println("Alarm List: " + requireView().findViewById(R.id.alarmList) as LinearLayout?)

Upvotes: 1

Andrew
Andrew

Reputation: 10182

You are looking in the wrong place for the view, you are trying to find it in the Activity scope with the line

println("Alarm List: " + activity!!.findViewById(R.id.alarmList) as LinearLayout?)

change that to

println("Alarm List: " + view!!.findViewById(R.id.alarmList) as LinearLayout?)

Upvotes: 1

Related Questions