return0
return0

Reputation: 215

Unresolved reference: section_label

I am learning android app development using KOTLIN and getting error Unresolved reference. Going by what error means I am referring to something that doesn't exist, like an undeclared variable name . But my layout XML file does contain the id with the label. Can somebody pls let me know what am I missing?

Here is my MainActivity.kt file

package com.example.chapapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    class PlaceholderFragment:Fragment(){
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            val rootView = inflater.inflate(R.layout.fragment_main,container,false)
            rootView.section_label.text = "Hello World from section ${arguments?.getInt(ARG_SECTION_NUMBER)}"
            return rootView
        }
     companion object {
        private val ARG_SECTION_NUMBER = "Section number"
        fun newIntent(sectionNumber: Int): PlaceholderFragment {
            val fragment = PlaceholderFragment()
            val args = Bundle()
            args.putInt(ARG_SECTION_NUMBER, sectionNumber)
            fragment.arguments = args
            return fragment
        }
    }

    }
} 

fragment_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Views: 583

Answers (1)

Zain
Zain

Reputation: 40830

rootView.section_label.text

Here you try to access a view using synthetic view binding that is provided by Kotlin Android Extensions Plugin.

This plugin is deprecated now, so instead you can use findViewById():

val section_label = rootView.findViewById<TextView>(R.id.section_label)
section_label.text = "Hello World from section ${arguments?.getInt(ARG_SECTION_NUMBER)}"

Or if you would like to use the deprecated plugin you need to make sure to:

Add the Kotlin Android Extensions plugin in build.gradle (module level)

plugins {
    id 'kotlin-android-extensions'
}

Then rebuild your project, and make sure that the synthetic library is imported in MainActivity something like:

import kotlinx.android.synthetic.main.activity_main.*

Upvotes: 1

Related Questions