sahil gupta
sahil gupta

Reputation: 23

"findViewById must not be null" error in kotlin android

In this activity i have taken three variables in kotlin with findViewById for the three spinner views in xml file. Whenever i run build the project it builds successfully but whenever i enter this activity the app stops and closes. and "run" log shows the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sahil.randoms/com.sahil.randoms.activity_name_gender}: java.lang.IllegalStateException: findViewById(R.id.newSpin) must not be null

it happens whenever i create the third spinner like this one. But runs perfectly for only two spinners.

Here is kotlin file:

package com.sahil.randoms

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Spinner

class activity_name_gender : AppCompatActivity() {

    lateinit var spnGender: Spinner
    lateinit var spnAlphabet: Spinner
    lateinit var newSpin: Spinner

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_name_gender)

        spnGender=findViewById(R.id.spnGender)
        spnAlphabet=findViewById(R.id.spnNation)
        newSpin= findViewById(R.id.newSpin)

        val Gender= arrayOf("Boy","Girl")
        val adapter1=ArrayAdapter(this, android.R.layout.simple_spinner_item ,Gender)
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spnGender.adapter=adapter1


        val Nation= arrayOf("Hindu","Muslim","Sikh","Christian")
        val adapter2: ArrayAdapter<String> = ArrayAdapter(this@activity_name_gender,android.R.layout.simple_spinner_item,Nation)
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spnAlphabet.adapter= adapter2


        val new= arrayOf("A","B","C","D","E")
        val adapter3: ArrayAdapter<String> = ArrayAdapter(this@activity_name_gender,android.R.layout.simple_spinner_item,new)
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        newSpin.adapter= adapter3

    }
}

xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".activity_name_gender">

    <TextView
        android:id="@+id/txtGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:padding="10dp"
        android:textSize="20sp"
        android:background="@drawable/background"
        android:text="Select The Gender"
        android:textAlignment="center"
        android:textColor="#0e2896"
        android:textStyle="bold"/>
    <Spinner
        android:id="@+id/spnGender"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:dropDownWidth="100dp"
        android:forceHasOverlappingRendering="false"
        android:layout_below="@id/txtGender"
        android:layout_centerHorizontal="true"
        android:textAlignment="center"
        android:background="@drawable/background"
        />
    <TextView
        android:id="@+id/txtNation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/spnGender"
        android:layout_marginTop="100dp"
        android:textSize="20sp"
        android:padding="10dp"
        android:background="@drawable/background"
        android:text="Select The Nationality"
        android:textAlignment="center"
        android:textColor="#0e2896"
        android:textStyle="bold"/>
    <Spinner
        android:id="@+id/spnNation"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:dropDownWidth="100dp"
        android:textAlignment="center"
        android:layout_below="@id/txtNation"
        android:layout_centerHorizontal="true"
        android:background="@drawable/background"
        />
    <Spinner
        android:id="@+id/newSpin"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:dropDownWidth="100dp"
        android:textAlignment="center"
        android:layout_below="@id/spnNation"
        android:layout_centerHorizontal="true"
        android:background="@drawable/background"
        />

</RelativeLayout>

Please help.. Thanks in advance :)

Upvotes: 1

Views: 5142

Answers (3)

cactustictacs
cactustictacs

Reputation: 19514

Everything looks fine in the code you've posted, if you're inflating the right layout file with setContentView - a view with that ID is in there, so findViewById shouldn't return null. Try running Clean in your Build menu, or if that doesn't work, Invalidate Caches / Restart in the File menu - sometimes the R file gets confused about IDs

Upvotes: 1

Muhammad Shujja
Muhammad Shujja

Reputation: 133

Firstly, In kotlin you don't have to find view by their ids, views are binded automatically from your layout which is defined in your activity/fragment, just get your view id directly in class like this,

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_name_gender)

        val Gender= arrayOf("Boy","Girl")
        val adapter1=ArrayAdapter(this, android.R.layout.simple_spinner_item ,Gender)
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spnGender.adapter=adapter1


        val Nation= arrayOf("Hindu","Muslim","Sikh","Christian")
        val adapter2: ArrayAdapter<String> = ArrayAdapter(this@activity_name_gender,android.R.layout.simple_spinner_item,Nation)
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spnNation.adapter= adapter2


        val new= arrayOf("A","B","C","D","E")
        val adapter3: ArrayAdapter<String> = ArrayAdapter(this@activity_name_gender,android.R.layout.simple_spinner_item,new)
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        newSpin.adapter= adapter3

    }

Secondly, Use recommended naming conventions, your activity name should be like ActivityNameGender, and your xml name is fine.

Finally, It will be like

class ActivityNameGender: AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_name_gender)

        val Gender= arrayOf("Boy","Girl")
        val adapter1=ArrayAdapter(this, android.R.layout.simple_spinner_item ,Gender)
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spnGender.adapter=adapter1


        val Nation= arrayOf("Hindu","Muslim","Sikh","Christian")
        val adapter2: ArrayAdapter<String> = ArrayAdapter(this@activity_name_gender,android.R.layout.simple_spinner_item,Nation)
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spnNation.adapter= adapter2


        val new= arrayOf("A","B","C","D","E")
        val adapter3: ArrayAdapter<String> = ArrayAdapter(this@activity_name_gender,android.R.layout.simple_spinner_item,new)
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        newSpin.adapter= adapter3

    }
}

Hope the answer to the question :)

Upvotes: 0

kostyabakay
kostyabakay

Reputation: 1689

If you are coding using Kotlin you don't have to use findViewById. Instead of this try synthetics. In your build.gradle file add this line apply plugin: 'kotlin-android-extensions' below apply plugin: 'kotlin-android'. After this, you will be able to refer to your views by their id as a variable. For example instead of this line spnGender=findViewById(R.id.spnGender) you can use just spnGender with synthetic import. For more detailed information read here.

Upvotes: 0

Related Questions