java.lang.NullPointerException on Button in android



这段代码以前工作正常,但现在我在按钮上出现空指针异常

import kotlinx.android.synthetic.main.activity_splash.*
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
button_splash_getting_started.setOnClickListener {
val intent = Intent(this@SplashActivity, RegisterActivity::class.java)
intent.putExtra("FROM_SPLASH_ACTIVITY",true)
startActivity(intent)
}
textView_splash_already_have_account.setOnClickListener {
val loginIntent = Intent(this@SplashActivity, LoginActivity::class.java)
loginIntent.putExtra("FROM_SPLASH_ACTIVITY",true)
startActivity(loginIntent)
}
}

错误 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener('

这是日志猫

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edubaba.howApp/com.test.ui.SplashActivity}: 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:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
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.edubaba.howApp.ui.splash.SplashActivity.onCreate(SplashActivity.kt:35)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)

这是 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/activity_splash_root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="customsnackbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/activity_splash_constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView_splash_background"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/how_background" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/activity_splash_skip_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:padding="20dp"
android:text="@string/skip"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/activity_splash_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/progressBarWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_splash_getting_started"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="16dp"
android:background="@drawable/background_button_getting_started"
android:text="@string/get_started"
android:textColor="#ec975d"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/textView_splash_already_have_account"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="798dp" />
<TextView
android:id="@+id/textView_splash_already_have_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="128dp"
android:text="@string/already_signed"
android:textColor="@color/white"
android:translationZ="1dp"
app:layout_constraintBottom_toBottomOf="@+id/activity_splash_constraintLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

谁能说出可能出了什么问题?

此 idbutton_splash_getting_started在您的 Xml 上不存在,这就是您收到错误的原因。

将此 idbutton_splash_getting_started替换为此button_splash_getting_started1

就是这样。

我想我发现了问题。在onCreate((方法中,你有:

setContentView(R.layout.activity_splash) 

但是您的.xml文件有 id:

android:id="@+id/activity_splash_root_layout"

所以,我认为你需要更换才能setContentView()正确的id="". 例如:

setContentView(R.id.activity_splash_root_layout)

最新更新