typecast java_kotlin.TypeCastException: null cannot be cast to non-null type com.midsizemango.databa...

在`EditNoteActivity`中,当尝试从Intent的extras中获取Serializable Extra `EXTRA_NOTE`并转换为Note对象时,遇到了`TypeCastException: null cannot be cast to non-null type com.midsizemango.databa...`。该问题发生在`onCreate`方法中,当尝试从Intent中获取Note对象并设置到变量`note`时。如果Note对象为null,应用将崩溃。解决方案可能包括确保在传递和接收Parcelable或Serializable对象时检查是否为null。
摘要由CSDN通过智能技术生成

class EditNoteActivity : AppCompatActivity() { var note: Note? = null private val editNote: TextView? = null private val fabdrwble: Boolean? = null private val notesData: MutableList? = null private var databaseHelper: DatabaseHelper? = null private val save: Boolean? = null private var saveButton: FloatingActionButton? = null private val tint: ColorStateList? = null internal var mRowId: Long? = null internal var spinner: Spinner? = null internal var spinnertext: String? = null internal var fav: Int = 0 internal var mSharedFromIntentFilter = false internal var editTitle: EditText? = null internal var editContent: EditText? = null internal var inputlayoutTitle: TextInputLayout? = null internal var inputlayoutContent: TextInputLayout? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_edit_note) var toolbar = findViewById(R.id.toolbar_edit) as Toolbar? setSupportActionBar(toolbar) if (supportActionBar != null) supportActionBar!!.setDisplayHomeAsUpEnabled(true) databaseHelper = DatabaseHelper(applicationContext) inputlayoutTitle = findViewById(R.id.inputlayoutTitle) as TextInputLayout? inputlayoutContent = findViewById(R.id.inputlayoutContent) as TextInputLayout? editTitle = findViewById(R.id.note_title) as EditText editContent = findViewById(R.id.note_content) as EditText? val bundle = intent.extras val s = bundle.getString("edit") if (s == "add") { window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) } else if (s == "editv") { window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) } note = intent.getSerializableExtra(EXTRA_NOTE) as Note if (note != null) { editTitle?.setText(note!!.getTitle()) editContent?.setText(note!!.getContent()) } else { note = Note() //note.setUpdatedAt(new Date()); } saveButton = findViewById(R.id.add_edit_button) as FloatingActionButton? saveButton!!.setOnClickListener { if (isNoteFormOk) { setNoteResult() finish() } else validateNoteForm() } var ll = findViewById(R.id.llmain) as LinearLayout? var ll1 = findViewById(R.id.ll1) as LinearLayout? /*if(note.getColor() == Color.TRANSPARENT){ selectedColor = preselect; }else { selectedColor = note.getColor(); } getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); systemBarTintManager = new SystemBarTintManager(this); systemBarTintManager.setStatusBarTintEnabled(true); ll.setBackgroundColor(selectedColor); ll1.setBackgroundColor(selectedColor); toolbar.setBackgroundColor(note.getColor()); systemBarTintManager.setStatusBarTintColor(selectedColor);*/ } override fun onResume() { super.onResume() } override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { android.R.id.home -> { onBack() return true } /* case R.id.speech: try { displaySpeechRecognizer(); } catch (ActivityNotFoundException e) { Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://market.android.com/details?id=com.google.android.googlequicksearchbox")); startActivity(browserIntent); } return true;*/ else -> return super.onOptionsItemSelected(item) } } private fun displaySpeechRecognizer() { val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH) intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) startActivityForResult(intent, SPEECH_REQUEST_CODE) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { if (requestCode == SPEECH_REQUEST_CODE && resultCode == Activity.RESULT_OK) { val results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) val spokenText = results[0] editContent?.setText(spokenText) } if (requestCode == RequestResultCode.REQUEST_CODE_ADD_NOTE) { if (resultCode == Activity.RESULT_OK) { addNote(data) } } } private val isNoteFormOk: Boolean get() { val title = editTitle?.text.toString() return !(title == null || title.trim { it setNoteResult() finish() }.setNegativeButton("CANCEL") { dialog, which -> setResult(Activity.RESULT_CANCELED, Intent()) finish() }.show() } } else { setResult(Activity.RESULT_CANCELED, Intent()) finish() } } private fun addNote(data: Intent) { val note = data.getSerializableExtra(EXTRA_NOTE) as Note val noteId = databaseHelper!!.createNote(note) note.setId(noteId) } override fun onBackPressed() { onBack() val intentHome = Intent(this@EditNoteActivity, MainActivity::class.java) intentHome.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP intentHome.putExtra(EXTRA_NOTE, note) setResult(Activity.RESULT_OK, intentHome) } companion object { private val EXTRA_NOTE = "EXTRA_NOTE" private val SPEECH_REQUEST_CODE = 0 fun isNullOrBlank(str: String?): Boolean { return str == null || str.trim { it

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值