如何解决数据库处理程序出现问题会使应用程序崩溃kotlin
我正在创建一个应用,该应用允许您将项目添加到片段内的列表中。 现在,每次编译器到达调用函数的数据库处理程序时,应用程序崩溃,而我找不到原因。 shopping_list.kt
package com.example.pricetag.fragments
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.example.pricetag.R
import com.example.pricetag.fragments.DTO.ItemList
import kotlinx.android.synthetic.main.fragment_shopping_list.*
class shopping_list : Fragment() {
lateinit var dbHandler: DBHandler
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
var view = inflater.inflate(R.layout.fragment_shopping_list,container,false)
val addItem_button = view.findViewById<com.google.android.material.floatingactionbutton.FloatingActionButton>(R.id.fab_shoppiglist)
addItem_button.setonClickListener {
val dialog = AlertDialog.Builder(getContext())
val tempView = layoutInflater.inflate(R.layout.dialog_shoppinglist,null)
val text_result = tempView.findViewById<EditText>(R.id.et_Itemlist)
dialog.setView(tempView)
dialog.setPositiveButton("Add") { _: DialogInterface,_: Int ->
if (text_result.text.isNotEmpty()) {
val itemList = ItemList()
itemList.name = text_result.text.toString()
dbHandler.addItem(itemList)
}
}
dialog.setNegativeButton("Cancel"){ _: DialogInterface,_: Int->
}
dialog.show()
}
return view
}
private fun refreshList(){
rv_shoppinglist.adapter = ShoppinglistAdpter(this.requireContext(),dbHandler.getItem())
}
class ShoppinglistAdpter(val context: Context,val list: MutableList<ItemList>): RecyclerView.Adapter<ShoppinglistAdpter.ViewHolder>(){
class ViewHolder(v : View) : RecyclerView.ViewHolder(v){
val itemName : TextView = v.findViewById(R.id.tv_item_name)
}
override fun onCreateViewHolder(p0: ViewGroup,pl: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.rv_child_shoppinglist,p0))
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: ViewHolder,p1: Int) {
holder.itemName.text = list[p1].name
}
}
}
DBHandler.kt
package com.example.pricetag.fragments
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.sqliteDatabase
import android.database.sqlite.sqliteOpenHelper
import android.util.Log
import com.example.pricetag.fragments.DTO.ItemList
class DBHandler(val context: Context) : sqliteOpenHelper(context,DB_NAME,null,DB_VERSION){
override fun onCreate(db: sqliteDatabase){
val createItemTable = "CREATE TABLE $TABLE_ITEMS (" +
"$COL_ID integer PRIMARY KEY AUTOINCREMENT," +
"$COL_NAME varchar," +
"$COL_AMOUNT integer);"
db.execsql(createItemTable)
}
override fun onUpgrade(p0: sqliteDatabase?,p1: Int,p2: Int) {
}
fun addItem(itemList : ItemList) : Boolean {
Log.i("Err","#9")
val db = writableDatabase
val cv = ContentValues()
cv.put(COL_NAME,itemList.name)
val result = db.insert(TABLE_ITEMS,cv)
return result != (-1).toLong()
}
fun getItem() : MutableList<ItemList>{
val result : MutableList<ItemList> = ArrayList()
val db : sqliteDatabase = readableDatabase
val queryResult = db.rawQuery("SELECT * from $TABLE_ITEMS",null)
if(queryResult.movetoFirst()){
do{
val itemList = ItemList()
itemList.id = queryResult.getLong(queryResult.getColumnIndex(COL_ID))
itemList.name = queryResult.getString(queryResult.getColumnIndex(COL_NAME))
itemList.amount = queryResult.getInt(queryResult.getColumnIndex(COL_AMOUNT))
result.add(itemList)
} while(queryResult.movetoNext())
}
return result
}
}
ItemList.kt
package com.example.pricetag.fragments.DTO
class ItemList {
var id: Long = -1
var name: String = ""
var amount = 0
}
Const.kt
package com.example.pricetag.fragments
const val DB_NAME = "ItemList"
const val DB_VERSION = 1
const val TABLE_ITEMS = "Items"
const val COL_ID = "id"
const val COL_NAME = "name"
const val COL_AMOUNT = "amount"
请帮助我找到问题并解决。 欣赏它。祝你有美好的一天
编辑:这是美眉记录(错误
2020-08-28 03:42:23.932 6619-6619/com.example.pricetag E/RecyclerView: No adapter attached; skipping layout
2020-08-28 03:42:26.710 6619-6619/com.example.pricetag E/RecyclerView: No adapter attached; skipping layout
2020-08-28 03:42:26.740 6619-6619/com.example.pricetag E/RecyclerView: No adapter attached; skipping layout
2020-08-28 03:42:30.059 6619-6619/com.example.pricetag E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pricetag,PID: 6619
kotlin.UninitializedPropertyAccessException: lateinit property dbHandler has not been initialized
at com.example.pricetag.fragments.shopping_list.getDbHandler(shopping_list.kt:20)
at com.example.pricetag.fragments.shopping_list$onCreateView$1$1.onClick(shopping_list.kt:38)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
这是美眉日志(调试):
2020-08-28 03:42:30.059 6619-6619/com.example.pricetag E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pricetag,PID: 6619
kotlin.UninitializedPropertyAccessException: lateinit property dbHandler has not been initialized
at com.example.pricetag.fragments.shopping_list.getDbHandler(shopping_list.kt:20)
at com.example.pricetag.fragments.shopping_list$onCreateView$1$1.onClick(shopping_list.kt:38)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
解决方法
此处不包含
初始化代码。 例如)
onCreateView(){
...
dbHandler = DBHandler(getActivity())
...
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。