Search code examples
androidkotlinkotlin-android-extensions

Unresolved referenced. Android kotlin


I have started learning Kotlin for android and I created a test project with 2 fragments. The first fragment works well and all controls i.e buttons textviews are accessible but in 2nd fragment none of the controls are accessible and gives error of unresolved reference. Here is my code of fragment.

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.frag_registration.*


class FragRegistration : Fragment() {

    private var rootView: View? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        rootView = inflater.inflate(R.layout.frag_registration, container, false)
        initViews()
        initEvents()

        return rootView
    }

    private fun initViews() {
      rootView.btnRegister.setOnClickListener { // error on this line
            Log.d(TAG,"Button click event")
        }
    }

    private fun initEvents() {

    }



}

This is my layout 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    tools:context=".FragRegistration">

    <ImageView
        android:id="@+id/companyLogo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:contentDescription="@null"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/etMobileNumber"
        android:layout_width="0dp"
        android:layout_height="@dimen/_60sdp"
        android:background="@drawable/line_round_background"
        android:gravity="center"
        android:hint="@string/enter_mobile_num"
        android:inputType="number"
        android:maxLength="11"
        android:maxLines="1"
        android:paddingLeft="@dimen/_8sdp"
        android:paddingRight="@dimen/_8sdp"
        android:textColorHint="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatImageButton
        android:id="@+id/btnRegister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="?attr/selectableItemBackground"
        app:layout_constraintBottom_toBottomOf="@+id/etMobileNumber"
        app:layout_constraintEnd_toEndOf="@+id/etMobileNumber"
        app:layout_constraintTop_toTopOf="@+id/etMobileNumber"
        app:srcCompat="@drawable/ic_pink_right_arrow" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_5sdp"
        android:gravity="center"
        android:text="@string/pin_via_sms"
        app:layout_constraintEnd_toEndOf="@+id/etMobileNumber"
        app:layout_constraintStart_toStartOf="@+id/etMobileNumber"
        app:layout_constraintTop_toBottomOf="@+id/etMobileNumber" />

    <androidx.appcompat.widget.AppCompatButton
        android:layout_width="wrap_content"
        android:layout_height="@dimen/_25sdp"
        android:layout_marginBottom="@dimen/_10sdp"
        android:background="@drawable/line_round_background"
        android:gravity="center"
        android:paddingLeft="@dimen/_10sdp"
        android:paddingRight="@dimen/_10sdp"
        android:text="@string/not_a_company_user"
        android:textColor="@color/darkGrey"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="@+id/guideline" />


    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/_8sdp"
        android:gravity="center"
        android:text="@string/mbb_number_reg"
        android:textColor="@color/colorAccent"
        app:layout_constraintBottom_toTopOf="@+id/tvCon"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/guideline" />

    <TextView
        android:id="@+id/tvCon"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/_8sdp"
        android:gravity="center"
        android:text="@string/terms_conditions"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="@+id/guideline" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="@dimen/_12sdp" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="@dimen/_12sdp" />

</androidx.constraintlayout.widget.ConstraintLayout>

I have already added kotlin extensions in build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
        applicationId "com.app.dev"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    --
    --
    --
}

This is the complete error enter image description here


Solution

  • Replace

    import kotlinx.android.synthetic.main.frag_registration.* 
    

    With

    import kotlinx.android.synthetic.main.frag_registration.view. *