单击 SaveTask 按钮后,未添加到 Main Activity 中的任务显示。并且应用程序正在崩溃。请检查布局管理器是不是正确设置
Posted
技术标签:
【中文标题】单击 SaveTask 按钮后,未添加到 Main Activity 中的任务显示。并且应用程序正在崩溃。请检查布局管理器是不是正确设置【英文标题】:After Click on SaveTask Button, Task in not adding in Main Activity display. and App is crashing. Please check layout manager is properly set or not单击 SaveTask 按钮后,未添加到 Main Activity 中的任务显示。并且应用程序正在崩溃。请检查布局管理器是否正确设置 【发布时间】:2021-10-28 09:58:30 【问题描述】:我们查看了一些帖子并尝试了一些没有结果。 这是完整代码的Github link。
这里是activity_main.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_
android:layout_
tools:context=".activity.MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/todoRv"
android:layout_
android:layout_
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/AddTask"
android:layout_
android:layout_
android:clickable="true"
app:srcCompat="@android:drawable/ic_input_add"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="25dp"
android:onClick="openNewTask"
/>
</RelativeLayout>
这里是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_
android:layout_
android:orientation="vertical"
tools:context=".activity.TaskActivity">
<TextView
android:id="@+id/txtTaskName"
android:layout_
android:layout_
android:layout_weight="1"
android:text="Task Name"
android:layout_margin="14dp"
android:textSize="33sp"/>
<EditText
android:id="@+id/etTaskName"
android:layout_
android:layout_
android:layout_weight="1"
android:ems="10"
android:hint="Please Enter Task Name"
android:layout_margin="14dp"
android:textSize="20sp"
android:inputType="textPersonName"
/>
<Button
android:id="@+id/saveTask"
android:layout_
android:layout_gravity="center_horizontal"
android:layout_marginTop="34dp"
android:layout_
android:layout_weight="1"
android:onClick="saveTask"
android:text="Save Task" />
</LinearLayout>
这里是 item_todo.xml 的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_>
<TextView
android:id="@+id/txtShowTitle"
android:layout_
android:layout_
android:layout_marginTop="25dp"
android:layout_marginLeft="15dp"
android:textSize="35sp"
android:text="Task Title Name" />
</LinearLayout>
这是 MainActivity.kt 的代码
package abc.com.onetaskadd.activity
import abc.com.onetaskadd.R
import abc.com.onetaskadd.adapter.TodoAdapter
import abc.com.onetaskadd.database.TodoDatabase
import abc.com.onetaskadd.model.TodoModel
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Adapter
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity()
val todoList = arrayListOf<TodoModel>()
var recyclerAdapter= TodoAdapter(todoList)
lateinit var layoutmanger: RecyclerView.LayoutManager
lateinit var todoRv: RecyclerView
val db by lazy
TodoDatabase.getDatabase(this)
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
todoRv= findViewById(R.id.todoRv)
todoRv.apply
layoutmanger= LinearLayoutManager(this@MainActivity)
adapter=this@MainActivity.recyclerAdapter
db.todoDao().getAllTask().observe(this, Observer
if (!it.isNullOrEmpty())
todoList.clear()
todoList.addAll(it)
recyclerAdapter.notifyDataSetChanged()
)
fun openNewTask(view: View)
startActivity(Intent(this, TaskActivity::class.java))
这是 TaskActivity.kt 的代码
package abc.com.onetaskadd.activity
import abc.com.onetaskadd.R
import abc.com.onetaskadd.database.TodoDatabase
import abc.com.onetaskadd.model.TodoModel
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.SyncStateContract.Helpers.insert
import android.view.View
import android.widget.Button
import android.widget.EditText
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
const val DB_NAME = "todo.db"
class TaskActivity : AppCompatActivity()
lateinit var etTaskName: EditText
lateinit var saveTask: Button
val db by lazy
TodoDatabase.getDatabase(this)
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_task)
etTaskName = findViewById(R.id.etTaskName)
saveTask= findViewById(R.id.saveTask)
fun saveTask(view: View)
val taskName = etTaskName.editableText.toString()
GlobalScope.launch(Dispatchers.Main)
val id = withContext(Dispatchers.IO)
return@withContext db.todoDao ().insertTask(
TodoModel(
taskName
)
)
finish()
这是 TodoAdapter.kt 的代码
package abc.com.onetaskadd.adapter
import abc.com.onetaskadd.R
import abc.com.onetaskadd.model.TodoModel
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
class TodoAdapter (val todoList: List<TodoModel>) :RecyclerView.Adapter<TodoAdapter.TodoViewHolder>()
class TodoViewHolder(view: View):RecyclerView.ViewHolder(view)
fun bind(todoModel: TodoModel)
with(itemView)
val txtShowTitle: TextView= findViewById(R.id.txtShowTitle)
txtShowTitle.text=todoModel.taskName
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder
val view= LayoutInflater.from(parent.context).inflate(R.layout.item_todo, parent, false)
return TodoViewHolder(view)
override fun onBindViewHolder(holder: TodoViewHolder, position: Int)
holder.bind(todoList[position])
override fun getItemCount(): Int
return todoList.size
这是 TodoDao.kt 的代码
package abc.com.onetaskadd.database
import abc.com.onetaskadd.model.TodoModel
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
@Dao
interface TodoDao
@Insert()
suspend fun insertTask(todoModel: TodoModel):Long
@Query("Select * From TodoModel where isFinished != -1 ")
fun getAllTask():LiveData<List<TodoModel>>
这是 TaskDatabase.kt 的代码
package abc.com.onetaskadd.database
import abc.com.onetaskadd.activity.DB_NAME
import abc.com.onetaskadd.model.TodoModel
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
@Database(entities = [TodoModel::class],version = 1)
abstract class TodoDatabase: RoomDatabase()
abstract fun todoDao(): TodoDao
companion object
@Volatile
private var INSTANCE: TodoDatabase? =null
fun getDatabase(context: Context): TodoDatabase
val tempInsance = INSTANCE
if (tempInsance != null)
return tempInsance
synchronized(this)
val instance= Room.databaseBuilder(
context.applicationContext,
TodoDatabase:: class.java,
DB_NAME
).build()
INSTANCE= instance
return instance
这是 TodoModel.kt 的代码
package abc.com.onetaskadd.model
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity
data class TodoModel(
val taskName: String,
var isFinished: Int =-1,
@PrimaryKey
var id:Long=0
)
【问题讨论】:
能否添加Logcat输出或者你得到的错误信息, 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。 【参考方案1】:您应该在 TaskDatabase.kt 文件中替换此代码
synchronized(this)
val instance= Room.databaseBuilder(
context.applicationContext,
TodoDatabase:: class.java,
DB_NAME
).fallbackToDestructiveMigration().build()
它将解决调试问题。
【讨论】:
请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。以上是关于单击 SaveTask 按钮后,未添加到 Main Activity 中的任务显示。并且应用程序正在崩溃。请检查布局管理器是不是正确设置的主要内容,如果未能解决你的问题,请参考以下文章
单击按钮后,Bootstrap5 模态未在 React.js 中显示