java.lang.InstantiationException: java.lang.Class<com.example.exampleproject.ExampleActivity> 没有零参数构造函数
java.lang.InstantiationException: java.lang.Class<com.example.exampleproject.ExampleActivity> has no zero argument constructor
当我 运行 我的应用程序向我显示(InstantiationException:没有零参数构造函数)并要求创建一个零参数构造函数时,我是 kotlin 的新手,请告诉我如何解决它。
登录 Activity:-
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:-
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 添加参数,应该是
class LoginActivity: AppCompatActivity(), AuthListenner {
//...
}
当我 运行 我的应用程序向我显示(InstantiationException:没有零参数构造函数)并要求创建一个零参数构造函数时,我是 kotlin 的新手,请告诉我如何解决它。 登录 Activity:-
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:-
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 添加参数,应该是
class LoginActivity: AppCompatActivity(), AuthListenner {
//...
}