0

When i run my app its showing me (InstantiationException:has no zero argument constructor) and ask for create a zero argument constructor, I am new in kotlin please suggest me how i can solve it. Login 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)
    }
}

This is my Model 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)

}

Amit Sharma
  • 261
  • 1
  • 4
  • 20
  • 1
    As the error implies, Android activities can't have any contributor parameters. They need a no-argument constructor to be instantiated by the system, it can't provide inputs it knows nothing about. – Henry Twist Apr 14 '21 at 01:03
  • Also make sure you do a quick Google search before you post a question here. Copying your title gives me [this](https://stackoverflow.com/q/35380266/10082297) duplicate question as the first result. – Henry Twist Apr 14 '21 at 01:15
  • I checked this one but it can't resolve my issue, if you know please can you write an answer for me. – Amit Sharma Apr 14 '21 at 01:34
  • Why doesn't it resolve it? – Henry Twist Apr 14 '21 at 01:46
  • why add (val ViewModelProvider: Any) after LoginActivity? – zhangxaochen Apr 14 '21 at 02:38
  • Your `ExampleActivity` code isn't included here, so it's hard to know for sure. – Ryan M Apr 17 '21 at 00:00

1 Answers1

1

Can't add para to the AppCompatActivity, it should be

class LoginActivity: AppCompatActivity(), AuthListenner {
    //...
}
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • its working fine thank you, One more think please can you guide me how i can call onSuccess method on button click. Check my LoginActivity. – Amit Sharma Apr 14 '21 at 14:09
  • hi@Amit Sharma, it's not a good place to answer it here.Maybe you can create a new question by clicking [Ask Question](https://stackoverflow.com/questions/ask). – zhangxaochen Apr 15 '21 at 00:50