当我运行我的应用程序时,它显示给我(InstantiationException:没有零参数构造函数(,并要求创建一个零参数构造函数,我是kotlin的新手,请建议我如何解决它。登录活动:-
class LoginActivity(val ViewModelProvider: Any) : AppCompatActivity(), AuthListenner {
private val listview: ListView? = null
private val progressBar: ProgressBar? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
}
override fun onStarted() {
toast("Login Started")
}
override fun onSuccess(logingResponse: LiveData<String>) {
toast("Login Success")
}
override fun onFailure(message: String) {
toast(message)
}
}
这是我的型号类别:-
class AuthViewModel() : ViewModel() {
var username: String? = null
var password:String? = null
var authListenner: AuthListenner? = null
fun onLoginButtonClick(View: View){
authListenner?.onStarted()
if (username.isNullOrEmpty() || password.isNullOrEmpty()){
authListenner?.onFailure("Invalid username or password")
return
}
val logingResponse = UserRepository().userLogin(username!!, password!!)
authListenner?.onSuccess(logingResponse)
}
}
class UserRepository {
fun userLogin(username: String, password: String) : LiveData<String> {
val loginResponse = MutableLiveData<String>()
MyApi().userLogin(username, password)
.enqueue(object : Callback<ResponseBody> {
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
loginResponse.value = t.message
}
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
if (response.isSuccessful){
loginResponse.value = response.body()?.string()
}else{
loginResponse.value = response.errorBody()?.string()
}
}
})
return loginResponse
}
}
interface AuthListenner {
fun onStarted()
fun onSuccess(logingResponse: LiveData<String>)
fun onFailure(message: String)
}
不能向AppCompatActivity添加para,它应该是
class LoginActivity: AppCompatActivity(), AuthListenner {
//...
}