如何解决Kotlin Android中的“ findViewById不能为空”错误
在此活动中,我在kotlin中使用了findViewById作为xml文件中三个旋转器视图的三个变量。 每当我运行构建项目时,它都会成功构建,但是每当我进入此活动时,应用程序就会停止并关闭。并且“运行”日志显示以下错误:
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,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,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-extensions'
下添加apply plugin: 'kotlin-android'
行。此后,您将能够通过其ID引用您的视图作为变量。例如,除了spnGender=findViewById(R.id.spnGender)
这行外,您还可以仅将spnGender
与synthetic
导入一起使用。有关更多详细信息,请阅读here。
首先,在kotlin中,您不必按其ID查找视图,视图是从活动/片段中定义的布局中自动绑定的,只需像这样直接在类中获取视图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,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,new)
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
newSpin.adapter= adapter3
}
第二,使用推荐的命名约定,您的活动名称应类似于ActivityNameGender,并且您的xml名称很好。
最后,就像
class ActivityNameGender: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_name_gender)
val Gender= arrayOf("Boy",new)
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
newSpin.adapter= adapter3
}
}
希望问题的答案:)
,如果您使用setContentView
来填充正确的布局文件,那么一切在发布的代码中看起来都还不错-其中包含该ID的视图,因此findViewById
不应返回null 。尝试在Clean
菜单中运行Build
,或者如果行不通,请在Invalidate Caches / Restart
菜单中运行File
-有时R
文件会混淆ID
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。