简体   繁体   English

java.lang.IllegalStateException:nicknameTextView 不能为 null

[英]java.lang.IllegalStateException: nicknameTextView must not be null

I´m trying to do this AboutMe example, but the application keeps crashing and send the error message: java.lang.IllegalStateException: nicknameTextView must not be null.我正在尝试执行此 AboutMe 示例,但应用程序不断崩溃并发送错误消息:java.lang.IllegalStateException:nicknameTextView 不能为空。

I don't understand why nicknameTextView always result null.我不明白为什么nicknameTextView 总是结果为空。 I added the println command to see the result from editText and it works.我添加了 println 命令来查看来自 editText 的结果并且它有效。 I know it has something to do with null value and initialize the variable somehow by reading other posts, however I don't get it.我知道它与空值有关,并通过阅读其他帖子以某种方式初始化变量,但是我不明白。

Thanks.谢谢。

package com.example.aboutme

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.done_button).setOnClickListener(View.OnClickListener {
            addNickname(it)
        })
    }
    private fun addNickname(it: View?) { // funcion para añadir el nick tecleado
        val editText = findViewById<EditText>(R.id.nickname)    // Lee el Nick Tecleado
        val nicknameTextView =
            findViewById<TextView>(R.id.nickname_text)   //Define el texto donde se va a escribir el nick tecledo

        println(editText.text)
        nicknameTextView.text = editText.text
        editText.visibility = View.GONE
        done_button.visibility = View.GONE
        nicknameTextView.visibility = View.VISIBLE
    }
}

MAIN ACTIVITY

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:paddingStart="@dimen/padding"
    android:paddingEnd="@dimen/padding"
    android:textAlignment="viewStart">

    <TextView
        android:id="@+id/name_text"
        style="@style/NameStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textAlignment="center" />

    <EditText
        android:id="@+id/nickname"
        style="@style/NameStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/What_is_your_nickname"
        android:inputType="textPersonName"
        android:textAlignment="center" />

    <TextView
        android:id="@+id/nickname_text"
        style="@style/NameStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="none"
        android:textAlignment="center"
        android:visibility="gone" />

    <Button
        android:id="@+id/done_button"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/layout_margin"
        android:fontFamily="@font/roboto"
        android:text="@string/done" />

    <ImageView
        android:id="@+id/star_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/layout_margin"
        android:contentDescription="@string/yellow_star"
        app:srcCompat="@android:drawable/btn_star_big_on" />

    <ScrollView
        android:id="@+id/bio_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/Imagen1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:contentDescription="@string/Imagen1"
                app:srcCompat="@android:drawable/ic_menu_view" />

            <TextView
                android:id="@+id/bio_text"
                style="@style/NameStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:lineSpacingMultiplier="1.2"
                android:text="@string/bio_name" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

Instead of initializing it every time the button is clicked, why don't you initialize it once in onCreate?与其每次单击按钮时都初始化它,为什么不在 onCreate 中初始化一次呢? For instance:例如:

private lateinit var editText: EditText
private lateinit var nicknameTextView: TextView

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

    editText = findViewById(...)
    nicknameTextView = findViewById(...)
   
    ...
}

And then just call the variables whenever needed.然后在需要时调用变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:[TextView]不能为null - java.lang.IllegalStateException: [TextView] must not be null java.lang.IllegalStateException:cardProductItem不能为null - java.lang.IllegalStateException: cardProductItem must not be null java.lang.IllegalStateException: TextView 不能为 null (Android/Kotlin) - java.lang.IllegalStateException: TextView must not be null (Android/Kotlin) Kotlin Android扩展:java.lang.IllegalStateException:视图不能为null - Kotlin Android Extensions : java.lang.IllegalStateException: view must not be null 引起:java.lang.IllegalStateException:TextView 不能为空 - Caused by: java.lang.IllegalStateException: TextView must not be null 将onCreateAnimation FATAL EXCEPTION重写为java.lang.IllegalStateException:不能为null - override onCreateAnimation FATAL EXCEPTION to java.lang.IllegalStateException : must not be null RadioButton Get ( java.lang.IllegalStateException: selectedRadioButton 不能为 null ) - RadioButton Get ( java.lang.IllegalStateException: selectedRadioButton must not be null ) JUnit / Mockito:java.lang.IllegalStateException:“字段”不能为null - JUnit/Mockito: java.lang.IllegalStateException: “field” must not be null 错误 java.lang.illegalstateexception 请求 == null - Error java.lang.illegalstateexception request == null Kotlin-java.lang.IllegalStateException:ViewStub必须具有非null的ViewGroup viewParent - Kotlin - java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM