-3

I'm creating the login page for my application and it gives me the error of Cannot infer a type of this parameter. Please specify it explicitly.

It gives error only within error and response. Any clue why it is like that?

var rq: RequestQueue = Volley.newRequestQueue(this)
            var sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
                if(response.equals("0"))
                    Toast.makeText(this, "Login failed.", Toast.LENGTH_LONG).show()
                else

                var i = Intent(this,LoggedinActivity::class.java)
                startActivity(i)
                Toast.makeText(this, "User created successfully.", Toast.LENGTH_LONG).show()
            }, Response.ErrorListener {
                error ->
                Toast.makeText(this, error.message, Toast.LENGTH_LONG).show()
            })
            rq.add(sr)

The problem has been arised because I didn't check all the brackets correctly, thus if statements didn't work correctly and it was giving error because of that.

Sorry for the inconvenience. I wish you great week ahead. I checked the first answer as correct one.

4

2 Answers 2

1

It might be your lack of brackets around your if and else.

Try this:

val rq: RequestQueue = Volley.newRequestQueue(this)
        val sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
            if(response == "0") {
                Toast.makeText(this, "Login failed.", Toast.LENGTH_LONG).show()
            }
            else {
                val i = Intent(this, MainActivity::class.java)
                startActivity(i)
                Toast.makeText(this, "User created successfully.", Toast.LENGTH_LONG).show()
            }
        }, Response.ErrorListener {
            error ->
            Toast.makeText(this, error.message, Toast.LENGTH_LONG).show()
        })
        rq.add(sr)
3
  • 1
    It 100% makes a difference. If you don't scope your if or else statements then it will only execute the first statement. I tried your block of code and it wouldn't compile until I put brackets around them.
    – TomH
    Feb 16, 2019 at 11:59
  • 1
    if(condition) statement is perfectly valid, the issue is the else statement specifically, because it uses several lines. But that isn't the only issue with the code, and therefore does not fix it alone.
    – Zoe is on strike
    Feb 16, 2019 at 12:11
  • You were right, mate. Sorry for the inconvenience, your answer was correct and I marked it as correct one. Jul 31, 2019 at 11:40
1

The this should refer to activity context otherwise the code believes that it is the Response.Listener additionally as mentioned by user8159708 you need the brackets, at least to the else statement.

val rq: RequestQueue = Volley.newRequestQueue(this)
        val sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
            if(response == "0") {
                Toast.makeText(this@MainActivity, "Login failed.", Toast.LENGTH_LONG).show()
            }
            else {
                val i = Intent(this@MainActivity, MainActivity::class.java)
                startActivity(i)
                Toast.makeText(this@MainActivity, "User created successfully.", Toast.LENGTH_LONG).show()
            }
        }, Response.ErrorListener {
            error ->
            Toast.makeText(this@MainActivity, error.message, Toast.LENGTH_LONG).show()
        })
        rq.add(sr)

Edit:

Proper syntax in kotlin is this@MainActivity according to this post

1
  • @NijatMursali check my edit... the problem in you code is this you can simple remove all the logic with this and use a simple log message to test it... then find the way to add the proper Context. Without the rest class file we cannot guess where is the proper context.
    – madlymad
    Feb 16, 2019 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.