Home > OS >  findViewById(R.id.deleteButton) must not be null in kotlin
findViewById(R.id.deleteButton) must not be null in kotlin

Time:02-08

I'm attempting to create a button called "delete" in a flashcard application that will delete flashcards the user does not need. However, whenever I attempt to implement this button, I get the error "findViewById(R.id.deleteButton) must not be null". Normally I would have a gridview that would display my flashcard sets, and if I clicked on any of the sets, it would lead me to the flashcards in the set. Now that I've tried to implement a "delete" button inside of my flashcard activity (the activity that activates when i click on the flashcard sets), the android emulator crashes whenever I click on my flashcard sets.

.kt code:

class FlashcardSetDetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_linear)

    val recyclerLinear = 
findViewById<RecyclerView>(R.id.recyclerLinear)
    recyclerLinear.adapter = AdapterLinear(Flashcard.getHardcodedFlashcards())

    val deleteButton: Button = findViewById(R.id.deleteButton)

    deleteButton.setOnClickListener() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        }
    }
}

my xml code is the following:

<TextView
    android:id="@ id/my_textLinear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintEnd_toStartOf="@ id/my_textLinear2"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@ id/my_textLinear2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:textColor="@color/black"
    android:textSize="24sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@ id/my_textLinear"
    app:layout_constraintTop_toTopOf="parent" />


<Button
    android:id="@ id/deleteButton"
    android:layout_width="157dp"
    android:layout_height="88dp"
    android:text="Delete"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

Ignore the textviews. They just display my the title and the description of my flashcards. My button is what does not work. Can Somebody please help me with this error? Thanks.

CodePudding user response:

I suggest you use view binding because it is null-safe and also type-safe. Because I was also facing this issue then I am started to use view binding it is easy and the main point is it replaces findViewById()

CodePudding user response:

According to your code above, your delete button is defined on your flashcard page. And there is no delete button in your activity_linear.xml, so you got an error. You need to implement this button in your Flashcard adapter.

  •  Tags:  
  • Related