TransWikia.com

Android Studio EditText cannot be null Error

Stack Overflow Asked by smish_ on January 21, 2021

I just migrated to Kotlin and I wanted to do a very basic thing. Display some city names in ListView and add a city using an alert Dialog. This was my first time taking input from a dialog. However, the error "EditText cannot be null" always persists. I am attaching my code below, someone please help me.

dialogLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/itemInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter City" />

    </com.google.android.material.textfield.TextInputLayout>


</FrameLayout>

MainActivity.kt

package com.example.ktlistview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.*
import androidx.appcompat.app.AlertDialog
import com.google.android.material.snackbar.Snackbar

class MainActivity : AppCompatActivity() {

    var m_city: String = ""
    val cities = ArrayList<String>()
    lateinit var listAdapter: ArrayAdapter<String>
    lateinit var addItemDialog: AlertDialog.Builder

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        initData()

        val list = findViewById<ListView>(R.id.listView)
        listAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, cities)
        list.adapter = listAdapter

        addItemDialog = AlertDialog.Builder(this)
        addItemDialog.create()
        addItemDialog.setTitle("Add City")
        addItemDialog.setView(R.layout.dialoglayout)
        val editText: EditText? = findViewById<EditText>(R.id.itemInput)
        addItemDialog.setPositiveButton("Ok") { _, _ ->
            m_city = editText?.text.toString()
        }
        addItemDialog.setNegativeButton("Cancel") { _, _ ->
            Toast.makeText(this, "City not added", Toast.LENGTH_SHORT).show()
        }


    }

    private fun initData() {
        cities.add("Delhi")
        cities.add("Mumbai")
        cities.add("Chennai")
        cities.add("Bhubaneswar")
        cities.add("Pune")
        cities.add("Bangalore")
    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_add -> {
                addItemDialog.show()
                addItem()
            }
        }

        return true
    }

    private fun addItem() {
        cities.add("m_city")
        listAdapter.notifyDataSetChanged()
    }
}

3 Answers

The problem you using findViewById from activity and not from the dialog replace:

addItemDialog.setView(R.layout.dialoglayout)
val editText: EditText? = findViewById<EditText>(R.id.itemInput)

with:

val dialogView = LayoutInflater.from(this).inflate(R.layout.dialoglayout, null, false)
addItemDialog.setView(dialogView)
val editText: EditText? = dialogView.findViewById<EditText>(R.id.itemInput)

This way you keep the reference of the view you wanna display

Answered by Marcos Vasconcelos on January 21, 2021

Try it like this. I tested it and it works fine :)

private fun showDialog(context :Context) {
    val addItemDialog = AlertDialog.Builder(context)
    addItemDialog.create()
    addItemDialog.setTitle("Add City")
    val inflater = this.layoutInflater
    val dialogView = inflater.inflate(R.layout.dialoglayout, null)
    addItemDialog.setView(dialogView)
    val editText: EditText? =  dialogView.findViewById(R.id.itemInput)
    addItemDialog.setPositiveButton("Ok") { _, _ ->
        m_city = editText?.text.toString()
        Toast.makeText(this, "City added $m_city", Toast.LENGTH_SHORT).show()
    }
    addItemDialog.setNegativeButton("Cancel") { _, _ ->
        Toast.makeText(this, "City not added", Toast.LENGTH_SHORT).show()
    }
    addItemDialog.show()
}

Answered by F.Mysir on January 21, 2021

The error is that your code references the activity_main.xml when you are actually using the dialogLayout.xml file.

Solution. In your MainActivity.kt, change this:

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

To this:

super.onCreate(savedInstanceState)
setContentView(R.layout.dialogLayout)

Answered by SatvikVejendla on January 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP