Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

android - Keep getting a "Classifier 'SharedViewModel' does not have a companion object, and thus must be initialized here" error when using a view model

I'm getting two errors in my code. One is at the private val sharedView model, I keep getting an "Classifier 'SharedViewModel' does not have a companion object, and thus must be initialized here". Lastly, in both the lines of question.text = sharedViewmodel.question and question.text = sharedViewModel.answer I'm getting an "Type mismatch. Required: Editable! Found: String?" but when I try to change it to editable I then get an error below my button click listener for question/answer.text.toString(). Does anyone have any idea how to fix these? Here's my code:

    package com.example.quest

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.EditText
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.google.android.material.floatingactionbutton.FloatingActionButton

class SecondActivity : AppCompatActivity() {
    private val sharedViewModel: SharedViewModel by viewModels(SharedViewModel)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val question = findViewById<EditText>(R.id.question)
        val answer = findViewById<EditText>(R.id.answer)

        if(sharedViewModel.question != null) {
            question.text = sharedViewModel.question
        }

        if(sharedViewModel.answer != null) {
            answer.text = sharedViewModel.answer
        }

        findViewById<FloatingActionButton>(R.id.btn3).setOnClickListener {

            sharedViewModel.question = question.text.toString()
            sharedViewModel.answer = answer.text.toString()
            val questiontext = question.text.toString()
            val answertext = answer.text.toString()

            val returnIntent = Intent()
            returnIntent.putExtra("test", questiontext)
            returnIntent.putExtra("test2", answertext)
            setResult(Activity.RESULT_OK, returnIntent)

            finish()
        }
    }

    }

Viewmodel:

    package com.example.quest

import androidx.lifecycle.ViewModel

class SharedViewModel : ViewModel() {
    var question: String ?= null
    var answer: String ?= null

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just use by viewModels() without passing the name of the class. You don't need to pass anything when your ViewModel has an empty constructor.

Instead of question.text = sharedViewModel.question, use question.setText(sharedViewModel.question). Same thing with the other one. The problem is that there are multiple setText() methods for TextView defined in Java. Kotlin creates a property out of the one whose parameter type matches the return value of getText(), which returns an Editable instead of a String. So the property access syntax for TextView only works with Editables rather than Strings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...