kotlin.TypeCastException:null 不能转换为非 null 类型 com.midsizemango.databasekotlin.Note [英] kotlin.TypeCastException: null cannot be cast to non-null type com.midsizemango.databasekotlin.Note

查看:34
本文介绍了kotlin.TypeCastException:null 不能转换为非 null 类型 com.midsizemango.databasekotlin.Note的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Kotlin 中编写我的应用程序,但是当我在 EXTRA_NOTE 打开 EditNoteActivity 时,我可能无法将 null 强制转换为非 null 类型并且应用程序强制停止.

I am trying to code my app in Kotlin, but I am getting null cannot be casted to non-null type and app force stops when I open EditNoteActivity at EXTRA_NOTE probably.

非常感谢任何帮助

代码:

class EditNoteActivity : AppCompatActivity() {

var note: Note? = null

private val editNote: TextView? = null

private val fabdrwble: Boolean? = null
private val notesData: MutableList<Note>? = 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 <= ' ' }.length == 0)
    }

private fun validateNoteForm() {
    var msg: String? = null
    if (isNullOrBlank(editTitle?.text.toString())) {
        msg = "Title Required"
        inputlayoutTitle?.error = "Title is Missing"
    }
    if (msg != null) {
        Toast.makeText(applicationContext, msg, Toast.LENGTH_LONG).show()
    }
}

private fun setNoteResult() {
    note!!.setTitle(editTitle?.text.toString().trim { it <= ' ' })
    note!!.setContent(editContent?.text.toString().trim { it <= ' ' })
    //note.setUpdatedAt(new Date());
    val intent = Intent()
    intent.putExtra(EXTRA_NOTE, note)
    setResult(Activity.RESULT_OK, intent)
    //addNote(intent);

    Toast.makeText(this@EditNoteActivity, "Note Saved.", Toast.LENGTH_LONG).show()
}

private fun onBack() {
    if (isNoteFormOk) {
        if (editTitle?.text.toString() == note!!.getTitle() && editContent?.text.toString() == note!!.getContent()) {
            setResult(Activity.RESULT_CANCELED, Intent())
            finish()
        } else {
            AlertDialog.Builder(this@EditNoteActivity)
                    .setTitle("Save")
                    .setMessage("Do You Want to Save Note")
                    .setPositiveButton("SAVE") { dialog, which ->
                        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 <= ' ' }.length == 0
    }
}
}

日志:

java.lang.RuntimeException:无法启动活动组件信息{com.midsizemango.databasekotlin/com.midsizemango.databasekotlin.EditNoteActivity}:kotlin.TypeCastException:null 不能转换为非 null 类型com.midsizemango.databasekotlin.Note在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)在 android.app.ActivityThread.access$800(ActivityThread.java:144)在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)在 android.os.Handler.dispatchMessage(Handler.java:102)在 android.os.Looper.loop(Looper.java:135)在 android.app.ActivityThread.main(ActivityThread.java:5221)在 java.lang.reflect.Method.invoke(Native Method)在 java.lang.reflect.Method.invoke(Method.java:372)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)引起:kotlin.TypeCastException:null 不能转换为非 null输入 com.midsizemango.databasekotlin.Note在com.midsizemango.databasekotlin.EditNoteActivity.onCreate(EditNoteActivity.kt:82)在 android.app.Activity.performCreate(Activity.java:5933)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)在 android.app.ActivityThread.access$800(ActivityThread.java:144)在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)在 android.os.Handler.dispatchMessage(Handler.java:102)在 android.os.Looper.loop(Looper.java:135)在 android.app.ActivityThread.main(ActivityThread.java:5221)在 java.lang.reflect.Method.invoke(Native Method)在 java.lang.reflect.Method.invoke(Method.java:372)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694)

java.lang.RuntimeException: Unable to start activity ComponentInfo{ com.midsizemango.databasekotlin/com.midsizemango.databasekotlin.EditNoteActivity}: kotlin.TypeCastException: null cannot be cast to non-null type com.midsizemango.databasekotlin.Note at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: kotlin.TypeCastException: null cannot be cast to non-null type com.midsizemango.databasekotlin.Note at com.midsizemango.databasekotlin.EditNoteActivity.onCreate(EditNoteActivity.kt:82) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5221)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694)

推荐答案

在这一行:

note = intent.getSerializableExtra(EXTRA_NOTE) as Note

注意非-null 类型,因此对它的强制转换会触发空检查.由于您之后手动将 notenull 进行比较,因此您的意思可能是 安全转换运算符,如果表达式不是右侧指定的类型,则产生 null :

Note is a non-null type, so the cast to it triggers a null check. Since you're comparing note to null manually afterwards, probably you meant the safe cast operator, which yields null if the expression is not of the type specified in the right-hand side:

note = intent.getSerializableExtra(EXTRA_NOTE) as? Note

这篇关于kotlin.TypeCastException:null 不能转换为非 null 类型 com.midsizemango.databasekotlin.Note的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆