首页 > 解决方案 > 使用 setOnClickListener 并获取“潜在 NullPointerException。某些布局版本中缺少资源

问题描述

我正在使用以下代码来使用,setOnClickListener并且每次运行程序时它都会在运行之前崩溃。我得到“应用程序已停止”。

在 logcat 中,它给了我这个错误:

2019-04-02 16:03:26.184 6592-6592/com.example.swoosh E/AndroidRuntime:致命异常:主进程:com.example.swoosh,PID:6592 java.lang.RuntimeException:无法启动活动 ComponentInfo{ com.example.swoosh/com.example.swoosh.MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'

如果我取出setOnClickListener程序运行的代码部分。这是导致错误的代码部分。在此之下,我将发布切换按钮所在的 XML 布局部分。

我正在使用 Android Studio 3.3.2 我在这里缺少什么?

    getStartedBtn.setOnClickListener {
        val leagueIntent=Intent(this, leagueActivity::class.java)
        startActivity(leagueIntent)
    }


<Button android:text="@string/get_started"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:fontFamily="@font/montserrat"
            app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
            android:id="@+id/getStartedBtn" android:typeface="normal" android:textSize="14sp"
            android:textColor="@color/colorAccent" android:background="@drawable/swoosh_button"
            android:layout_marginBottom="24dp" app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/textView3"
            app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="0.929" />

//this is full welcomeActivity.kt file
package com.example.swoosh
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_welcome.*

class MainActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_welcome)   
    getStartedBtn.setOnClickListener {
      startActivity(Intent(this, LeagueActivity::class.java))
    }
   }
}
Side note: the "getStartedBtn" is highlighed yellow and when I hold mouse over it, it says "Potential Null Pointer exception.  The resource is missing in some of layout versions"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.swoosh">

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity android:name=".LeagueActivity">
    </activity>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts"/>
</application>

标签: androidxmlkotlin

解决方案


我让它工作了。我改变了两件事,不确定是做什么的,但是我运行了程序并且它起作用了:

  1. 根据 Bruno Diego Martins 早些时候关于在文件名上使用大写的建议,我将“welcomeActivity.kt”文件名更改为大写“WelcomeActivity.kt”。

  2. 我注意到我的一个布局文件有重复。该文件与我的 activity_welcome.xml 具有相同的名称,但它的末尾有一个 (16)。我查看了该文件并注意到该布局的切换按钮具有不同的名称 ID。也许 setOnClickListener 试图使用具有错误按钮名称的 (16) 文件,而不是我正在使用的具有正确名称 getStartedBtn 的原始文件。所以我删除了最后有 (16) 的文件。然后我运行了这个程序,它工作了。

感谢大家的帮助。这是我在这里的第一篇文章,我很惊讶(惊喜地)人们提供帮助的速度之快。这里很棒的社区。


推荐阅读