完整错误堆栈跟踪:
04-05 16:01:34.265 18746-18746 /? E / AndroidRuntime:致命异常: 主要 处理:com.example.mrathi.livedataviewmodel,PID:18746 java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.mrathi.livedataviewmodel / com.example.mrathi.livedataviewmodel.MainActivity}: kotlin.KotlinNullPointerException 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 引起:kotlin.KotlinNullPointerException 在com.example.mrathi.livedataviewmodel.MainActivity.onCreate(MainActivity.kt:40) 在android.app.Activity.performCreate(Activity.java:6679) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这是我的带有房间api的数据库类(在使用MVVM模型的Kotlin中)
@Database(entities = arrayOf(BorrowModel::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
companion object {
var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase {
if (INSTANCE == null) {
**INSTANCE = Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "borrow_db")
.build()** through target invocation exception
}
return INSTANCE as AppDatabase
}
}
abstract fun itemAndPersonModel(): BorrowModelDao
}
这是我用于观察UI更新的数据库更改的活动代码:
class MainActivity : AppCompatActivity() , View.OnLongClickListener {
override fun onLongClick(v: View?): Boolean {
val borrowModel = v!!.getTag() as BorrowModel
viewModel!!.deleteItem(borrowModel)
return true
}
var viewModel: BorrowedListViewModel? = null
// private var recyclerViewAdapter: RecyclerViewAdapter? = null
// private var recyclerView: RecyclerView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var users = ArrayList<BorrowModel>()
var obj_adapter = RecyclerViewAdapter(users, this);
recyclerView.layoutManager = LinearLayoutManager(this,
LinearLayout.VERTICAL, false)
recyclerView.adapter = obj_adapter
try {
viewModel =
ViewModelProviders.of(this).get(BorrowedListViewModel::class.java)
}catch ( e :Exception){
e.toString()
}
viewModel!!.getItemAndPersonList()!!.observe(this, object :
Observer<List<BorrowModel>?> {
override fun onChanged(t: List<BorrowModel>?) {
Log.v("OnChanged","OnChanged!!")
obj_adapter.addItems(t)
}
})
}
}
这是ViewModel
类:
class BorrowedListViewModel(application: Application) : AndroidViewModel(application) {
private var itemAndPersonList: LiveData<List<BorrowModel>>? = null
private var appDatabase: AppDatabase? = null
init {
appDatabase = AppDatabase.getDatabase(this.getApplication())
itemAndPersonList = appDatabase!!.itemAndPersonModel().getAllBorrowedItems()
}
fun getItemAndPersonList(): LiveData<List<BorrowModel>>? {
return itemAndPersonList
}
fun deleteItem(borrowModel: BorrowModel) {
deleteAsyncTask(appDatabase!!).execute(borrowModel)
}
class deleteAsyncTask internal constructor(private var db: AppDatabase) : AsyncTask<BorrowModel, Void, Void>() {
override fun doInBackground(vararg params: BorrowModel): Void? {
db.itemAndPersonModel().deleteBorrow(params[0])
return null
}
}
}