I have created Recycler view and I want to save todo item them. Process is : click on add icon then show another activity which is TaskActivity, Then Enter Task name And Click on Save Button. The error posted below Caused by: java.lang.NullPointerException: findViewById(R.id.btnSave) must not be null
We have looked at a number of posts and tried a few with no results.
Here is the code for the MainActivity.kt
package abc.com.recyclerviewuserinput
import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
lateinit var recyclerMain : RecyclerView
lateinit var layoutmanger: RecyclerView.LayoutManager
lateinit var btnSave: Button
val myList = ArrayList<String>()
// declare variable for the adapter
lateinit var recyclerAdapter: Adapter // here Adapter is name of Adapter class
// lateinit var editTaskName : EditText
override fun onCreate(savedInstanceState: Bundle?) {
// val view= layoutInflater.inflate(R.layout.single_row, container, false )
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerAdapter = Adapter(this, myList) // recycle Adapter
recyclerMain= findViewById(R.id.recyclerMain) // Recycler view
layoutmanger= LinearLayoutManager(this) // layout manager
var editTaskName= findViewById <EditText>(R.id.editTaskName)
btnSave= findViewById(R.id.btnSave)
btnSave.setOnClickListener {
myList.add(editTaskName.text.toString() )
}
//Now set layoutmanager and recycler Adapter with Recycler view
recyclerMain.adapter= recyclerAdapter
recyclerMain.layoutManager= layoutmanger
}
fun openNewTask(view: View) {
startActivity(Intent(this,TaskActivity:: class.java))
}
}
Here is the code for the TaskActivity.kt
package abc.com.recyclerviewuserinput
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class TaskActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_task)
}
}
Here is the code for the activity_task.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TaskActivity">
<EditText
android:id="@+id/editTaskName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Pankaj"
android:hint="@string/enter_task_name_here"
android:inputType="textPersonName"
/>
<Button
android:id="@+id/btnSave"
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20sp"
android:text="@string/add_to_task" />
</LinearLayout>
Here is the code for the activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerMain"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:onClick="openNewTask"
android:tint="@color/purple_500"
android:layout_height="wrap_content"
android:layout_margin="23dp"
android:src="@drawable/add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="UsingOnClickInXml" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the Adapter.kt
package abc.com.recyclerviewuserinput
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import org.w3c.dom.Text
class Adapter(val context: Context, val itemList : ArrayList<String>): RecyclerView.Adapter<Adapter.SampleViewHolder>() {
class SampleViewHolder (view: View): RecyclerView.ViewHolder(view)
{
val rowtextView: TextView= view.findViewById(R.id.txtRecyclerRowItem)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleViewHolder {
val view=LayoutInflater.from(parent.context).inflate(R.layout.single_row, parent,false)
return SampleViewHolder(view)
}
override fun onBindViewHolder(holder: SampleViewHolder, position: Int) {
val text= itemList[position]
holder.rowtextView.text= text
}
override fun getItemCount(): Int {
return itemList.size
}
}
Why indViewById(R.id.btnSave) must not be null is showing here. I have also tried through lateinit var declare but he was also showing error. So what to do here to reslove.