我使用以下代码来使用setOnClickListener
,每次运行该程序时,它都会在运行之前崩溃。我收到“应用程序已停止”。
在 logcat 中,它给了我这个错误:
2019-04-02 16:03:26.184 6592-6592/com.example.swoosh E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.swoosh, PID: 6592 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.swoosh/com.example.swoosh.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
如果我取出代码的 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>
最佳答案
我成功了。我改变了两件事,不确定是哪一件事做了,但我运行了程序并且它起作用了:
按照 Bruno Diego Martin 之前关于在文件名上使用大写字母的建议,我将“welcomeActivity.kt”文件名更改为大写字母“WelcomeActivity.kt”。
我注意到我的一个布局文件有重复项。该文件与我的 Activity_welcome.xml 具有相同的名称,只是它的末尾有一个 (16)。我查看了该文件,注意到该布局的切换按钮具有不同的名称 ID。也许 setOnClickListener 试图使用具有错误按钮名称的 (16) 文件,而不是我正在处理的具有正确名称 getStartedBtn 的原始文件。所以我删除了最后有(16)的文件。然后我运行了该程序并且它起作用了。
感谢大家的帮助。这是我在这里发表的第一篇文章,我对人们提供帮助的速度感到惊讶(惊喜)。这里很棒的社区。
关于android - 使用 setOnClickListener 并获取“Potential NullPointerException。某些布局版本中缺少资源”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55483668/