android - android中按钮上的java.lang.NullPointerException
问题描述
这段代码之前工作正常,但现在我在按钮上得到空指针异常
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)'
这是logcat
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 中不存在,这就是您收到错误的原因。
将此 id 替换button_splash_getting_started
为 this button_splash_getting_started1
。
而已 。
推荐阅读
- ios - Google Cast iOS 无法加载媒体
- xcode - “复制捆绑资源”和“复制文件”->“资源”有什么区别
- java - 如何在 IntelliJ Idea 上启动 Java RMI 服务器
- machine-learning - 如何使用机器学习算法进行事件预测,例如洪水预测?
- amazon-web-services - AWS EKS 节点内存/CPU 限制较低 (10%)
- sql - 使用条件取表的前 N 行
- c - 将字符串存储在变量中
- android - How to get AWS Pinpoint Analytics to use Wifi or Cellular data
- solr - Solr、OutOfMemoryErrors 和堆未释放
- laravel - 使用 vuejs 和 laravel 的权限和角色