"findViewById must not be null" kotlin 中的错误 android

"findViewById must not be null" error in kotlin android

在这个 activity 中,我在 kotlin 中使用 findViewById 获取了三个变量,用于 xml 文件中的三个微调器视图。 每当我 运行 构建项目时,它都会成功构建,但每当我输入 activity 时,应用程序就会停止并关闭。并且“运行”日志显示以下错误:

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

每当我创建第三个像这样的微调器时,它就会发生。 但是 运行 只适合两个微调器。

这是 kotlin 文件:

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 文件:

<?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>

请帮忙.. 提前致谢:)

如果您使用 Kotlin 进行编码,则不必使用 findViewById。而不是这个尝试 synthetics。在您的 build.gradle 文件中,在 apply plugin: 'kotlin-android' 下面添加此行 apply plugin: 'kotlin-android-extensions'。在此之后,您将能够通过 id 作为变量来引用您的视图。例如,您可以只使用 spnGendersynthetic 导入,而不是这一行 spnGender=findViewById(R.id.spnGender)。有关更多详细信息,请阅读 here

首先,在 kotlin 中,您不必通过 ID 查找视图,视图会自动从您在 activity/fragment 中定义的布局中绑定,只需直接在 [=22= 中获取您的视图 ID ] 像这样,

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

    }

其次,使用推荐的命名约定,你的activity名字应该像ActivityNameGender,你的xml名字就可以了。

最后会像

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

    }
}

希望问题的答案:)

您发布的代码中的一切看起来都很好,如果您使用 setContentView 扩充正确的布局文件 - 具有该 ID 的视图在那里,所以 findViewById 不应该return 空。在 Build 菜单中尝试 运行 Clean,或者如果这不起作用,请在 File 菜单中尝试 Invalidate Caches / Restart - 有时是 R 文件对 ID 感到困惑