【问题标题】:Android Kotlin findViewById(R.id.txt_email) must not be nullAndroid Kotlin findViewById(R.id.txt_email) 不能为空
【发布时间】:2022-01-19 23:33:06
【问题描述】:

我是学生,所以我是初学者,我确实在这里找到了这个问题,但我不明白如何在我的代码中解决这个问题
如您所见,对于了解 kotlin 的人来说,这是一个简单的代码 当我按下按钮注册时,我只想开始注册活动

这是mainActivity代码:

package com.example.myclass

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView


class MainActivity : AppCompatActivity() {


    private lateinit var txtEmail: TextView
    private lateinit var edtEmail: EditText
    private lateinit var txtPassword: TextView
    private lateinit var edtPassword: EditText
    private lateinit var btnContinue: Button
    private lateinit var btnSignUp: Button
    private lateinit var btnFcb: Button


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

        txtEmail=findViewById(R.id.txt_email)
        edtEmail=findViewById(R.id.edt_email)
        txtPassword=findViewById(R.id.txt_password)
        edtPassword=findViewById(R.id.edt_password)
        btnContinue=findViewById(R.id.btn_continue)
        btnSignUp=findViewById(R.id.btn_signup)
        btnFcb=findViewById(R.id.btn_fcb)

        btnSignUp.setOnClickListener {
            val intent=Intent(this,signUp::class.java)
            startActivity(intent)
        }
    }
}

这是 MainActivity 的 XML 代码:

<?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:layout_gravity="center_horizontal"
    android:background="@color/blue_ligth"
    android:orientation="vertical"

    tools:context=".MainActivity">


    <ImageView
        android:id="@+id/img_logo"
        android:layout_width="464dp"
        android:layout_height="164dp"
        android:layout_gravity="center"
        android:layout_marginTop="1dp"
        android:src="@drawable/nice" />

    <TextView
        android:id="@+id/txt_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:fontFamily="@font/rubik_bold"
        android:text="Email"
        android:textColor="@color/white"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/edt_email"
        android:layout_width="match_parent"
        android:layout_height="49dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="5dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/edt_backgound"
        android:hint="MyClass@gmail.com"
        android:textAlignment="center"
        android:textColorHint="@color/blue_ligth"
        android:textSize="26dp"
        android:backgroundTint="@color/white"
        />

    <TextView
        android:id="@+id/txt_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="20dp"
        android:fontFamily="@font/rubik_bold"
        android:text="Password"
        android:textColor="@color/white"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/edt_password"
        android:layout_width="match_parent"
        android:layout_height="49dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="5dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/edt_backgound"
        android:backgroundTint="@color/white"
        android:fontFamily="@font/rubik_bold"
        android:hint="●●●●●●●●●●●●●●"
        android:textAlignment="center"
        android:textColorHint="@color/blue_ligth"
        android:textSize="26dp" />

    <Button
        android:id="@+id/btn_continue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="100dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/bt_background"
        android:fontFamily="@font/rubik_bold"
        android:text="continue"
        android:textColor="@color/blue_ligth"
        android:textSize="20dp" />

    <Button
        android:id="@+id/btn_signup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/edt_backgound"
        android:fontFamily="@font/rubik_bold"
        android:text="sign up"
        android:textColor="@color/white"
        android:textSize="20dp" />

    <Button
        android:id="@+id/btn_fcb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/bt2_3_background"
        android:fontFamily="@font/rubik_bold"
        android:text="login with facebook"
        android:textColor="@color/white"
        android:textSize="20dp" />

这是图片中的错误:

这是有错误的代码:

这是错误代码:

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myclass, PID: 17363
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myclass/com.example.myclass.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.myclass.MainActivity.onCreate(MainActivity.kt:28)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
W/System: A resource failed to call close. 

【问题讨论】:

  • 你确定没有另一个布局叫做activity_main吗?有时会为垂直和水平方向创建两种布局。
  • 是的,如果您有相同布局的两个版本,您可能希望确保所有相同的视图 ID 在两个版本中。
  • 是时候使用ViewBinding了
  • @lyncx 我该如何检查它
  • val txtEmai: TextView= find(R.id.txt_email) jusr tryb 这样可能行得通

标签: android kotlin


【解决方案1】:

请查看“viewBinding”。在您的项目中实施它几乎不需要 15 分钟(也许更少)。它是为了解决这类问题而创建的

这次是我...

class MainActivity : AppCompatActivity() {

    //To add
    lateinit var binding: ActivityMainBinding
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //To add
        binding = ActivityMainBinding.inflate(layoutInflater)

        //To remove
//        setContentView(R.layout.activity_main)
//        txtEmail=findViewById(R.id.txt_email)
//        edtEmail=findViewById(R.id.edt_email)
//        txtPassword=findViewById(R.id.txt_password)
//        edtPassword=findViewById(R.id.edt_password)
//        btnContinue=findViewById(R.id.btn_continue)
//        btnSignUp=findViewById(R.id.btn_signup)
//        btnFcb=findViewById(R.id.btn_fcb)

        binding.btnSignup.setOnClickListener {
//            val intent= Intent(this,signUp::class.java)
//            startActivity(intent)
        }

    }
}

每次您想使用视图时,只需调用 [绑定。“Your_view_id_here”]

别忘了在你的 build.gradle(模块)文件中添加这个

android{
 //...

   buildFeatures {
       viewBinding true
   }
}


【讨论】:

    【解决方案2】:

    您需要检查两件事。

    1. 检查 ID 为 txt_email 的视图是否确实存在于您的布局文件中。

    2. 检查您是否错误地从另一个布局文件中导入了同名 ID。您可以通过查看导入行轻松确认这一点。检查您没有从完全不同的片段中导入 ID。


    另外,我认为您应该花时间了解 DataBinding 和 ViewBinding。这是避免这种情况的一种廉价方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 2018-05-04
      • 2021-06-27
      • 2022-01-09
      相关资源
      最近更新 更多