【问题标题】:(android.view.View$OnClickListener)' on a null object reference Kotlin(android.view.View$OnClickListener)' 在一个空对象引用 Kotlin
【发布时间】:2019-11-28 14:55:02
【问题描述】:

我知道以前有人问过很多类似的问题,但通常都是用 Java 编写的。我遵循了多个教程,但每次都出现相同的错误。我关注了this tutorial,它适用于其他脚本。

尝试this answer,但效果不佳。

这个问题是独一无二的,因为其他问题是用 java 编写的,这对我来说并不容易。

这是我的MainActivity

class MainActivity : AppCompatActivity() {

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

        NumberVerButton()
    }

    private fun NumberVerButton() {
        next1.setOnClickListener {
            startActivity(Intent(this, NumberVer::class.java))
        }
    }
}

错误如下。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.wyspiansky, PID: 20258
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wyspiansky/com.example.wyspiansky.NumberVer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

我只想通过单击一个按钮来移动到另一个活动。

【问题讨论】:

标签: android android-activity kotlin nullpointerexception


【解决方案1】:

您的next1 变量从未被声明或初始化。我希望这是在某处声明的,否则代码应该会出现编译错误。

因此我的观察是 next1 变量未初始化。您需要将next1 声明为按钮,然后使用findViewById 方法初始化next1 变量,以使用布局中的特定视图引用ID 标记此按钮。

class MainActivity : AppCompatActivity() {

    var next1: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        next1 = findViewById(R.id.button) as Button
        NumberVerButton()
    }

    private fun NumberVerButton() {
        next1?.setOnClickListener {
            startActivity(Intent(this, MainActivity::class.java))
        }
    }
}

希望对您有所帮助。

【讨论】:

  • 非常感谢您的帮助。但是现在我有 2 个语法错误。首先:next1 = findViewById(R.id.Button) as Button(R.id.Button 中的按钮是红色的)和 2. next1.setOnClickListener {(中间的点是红色的,它告诉我我可以用其中一个替换它?。或!!
  • 其实我想写一个示例代码。这可能有一些语法错误,我认为可以自己处理。
  • 我试图删除语法错误,请立即检查更新的代码。谢谢。
【解决方案2】:

首先,您需要一个对象来引用,查看您的源代码,您正在使用 next1 对象,该对象未在源代码的任何位置声明。还有谁知道那通常是什么类型的对象

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    //First you prepare your objects to use them
    private var text1: TextView? = null
    private var next1: Button? = null

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

        //here you assign your references
        //between the Layout xml file and your objects
        text1 = findViewById(R.id.textView1)
        next1 = findViewById(R.id.button1)

        //here you set the click listener to your object
        // remember, it need to be a View object, in this case
        // is a Button which button heirs all from the parent class View
        next1!!.setOnClickListener{

            text1!!.text = "Hello S K, your click listener Works"

        }
    }
}

您还需要在 android:id xml 属性中为每个对象设置具有相同 id 名称的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

基本上,您需要将任何 UI 布局对象附加到源代码以控制它们,如果您没有正确实例化您的对象,您将有 NullPointer 异常,因为您的源代码将尝试引用您的 next1 对象但是它从未被声明过,也没有被实例化。

评论:老实说,如果您想要好的实践,您应该考虑使用 Java 进行开发并继续使用 Kotlin,直到您真正掌握它为止。在某些时候,像我这样的所有老开发人员我们都需要使用 kotlin,但至少现在我不知道有人可以将所有旧源代码复制到 kotlin 而没有任何问题。

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多