Kotlin 尝试对空对象引用调用虚拟方法->;决定

Kotlin 尝试对空对象引用调用虚拟方法->;决定,kotlin,Kotlin,我试着用一个按钮从一个主要活动切换到另一个活动,但当我试着运行应用程序时,它会在点击应用程序后立即关闭。我在命令中发现错误消息: 无法启动活动组件信息{fr.amseu.mystretch/fr.amseu.mystretch.MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ImageButton.setOnClickListener(android.view.view$OnClic

我试着用一个按钮从一个主要活动切换到另一个活动,但当我试着运行应用程序时,它会在点击应用程序后立即关闭。我在命令中发现错误消息:
无法启动活动组件信息{fr.amseu.mystretch/fr.amseu.mystretch.MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ImageButton.setOnClickListener(android.view.view$OnClickListener)
我在Main活动中的代码如下:


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

        val button = findViewById<ImageButton>(R.id.imageButton)

        button.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent);} 

重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val按钮=findViewById(R.id.imageButton)
button.setOnClickListener{
val intent=intent(这是SecondActivity::class.java)
星触觉(意图);}
这是我的主要活动:

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/item_main"
    android:background="@color/light_red">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="100dp"
        android:layout_height="120dp"
        app:cardCornerRadius="8dp"
        app:cardElevation="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/lower_legs"
            android:contentDescription="@string/home_page_first_button" />

    </androidx.cardview.widget.CardView>

    <View
        android:id="@+id/view_separation"
        android:layout_width="match_parent"
        android:visibility="invisible"
        android:layout_height="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/name_item"
        style="@style/SubTitleTextStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/default_margin"
        android:text="@string/home_page_first_button"
        app:layout_constraintBottom_toTopOf="@+id/view_separation"
        app:layout_constraintStart_toEndOf="@+id/cardView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/description_item"
        style="@style/DefaultTextStyle"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@string/home_page_description_1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/name_item"
        app:layout_constraintTop_toBottomOf="@+id/view_separation" />



</androidx.constraintlayout.widget.ConstraintLayout>

对于我的回收者视图,我在我的页面中使用这个模型,下面的项目是兄弟项目,但我使用适配器HomeFragment更改了图像和文本。。。 有人能帮我吗? (这是我的第一个应用程序)
非常感谢!

这个布局中不存在一个带有这个ID的按钮。考虑使用视图绑定来避免这种问题。我不确定理解“TENFE04”,我的IMAIDENT按钮已经存在于我的主要布局中,它的ID是:IMAGEPATH?我会在点击第二个活动之后重定向该人。请仔细检查它。我也会把你的布局代码贴在这里让我们看一看。但是缺少ID或者忘记调用
setContentView
是我见过的
findViewById
返回null的唯一原因。当然,我使用它的时候是一个回收器视图,我不知道它是否匹配?它在卡片视图中是这样的:
,如果你知道怎么做的话我可以使用我的Recycler视图将用户重定向到按下图像后的不同页面吗?:)您将其与RecyclerView一起使用是什么意思?您可以编辑您的问题以添加
activity_main.xml
的全部内容吗?