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
559 views
in Technique[技术] by (71.8m points)

android - java.lang.IllegalStateException: You must either set a text or a view

I'm new to android and I keep getting this error while clicking on a button which process database requests. I don't know what's going on because everything was working yesterday and I didn't do any changes at all.

The errorlog:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 7510
java.lang.IllegalStateException: You must either set a text or a view
    at com.android.internal.util.Preconditions.checkState(Preconditions.java:173)
    at android.widget.Toast.show(Toast.java:188)
    at com.example.myapplication.LoginActivity$userLogin$stringRequest$3.onErrorResponse(LoginActivity.kt:106)
    at com.android.volley.Request.deliverError(Request.java:617)
    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:104)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

And the code errorlog refers to:

private fun userLogin() {
        val email: String = emailText.text.toString().trim()
        val password: String = passwordText.text.toString().trim()

        val stringRequest = object : StringRequest(
                Method.POST, URL_LOGIN,
                Response.Listener<String> { response ->
                    try {
                        val obj = JSONObject(response)
                        if (!obj.getBoolean("error")) {
                            SharedPrefManager.getInstance(applicationContext)
                                    ?.userLogin(
                                            obj.getInt("id"),
                                            obj.getString("email"))
                            Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG).show()

                        }else{
                            Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG).show()

                        }
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                },
                Response.ErrorListener { volleyError -> Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show() }) {
            @Throws(AuthFailureError::class)
            override fun getParams(): Map<String, String> {
                val params = HashMap<String, String>()
                params.put("email", email)
                params.put("password", password)
                return params
            }
        }

Code which is calling this function:

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.login_layout)

    buttonLogin.setOnClickListener {
        userLogin()
        this.finish()
    }
}

Honestly, I have no idea what this error means, if I need to provide more code please let me know.

question from:https://stackoverflow.com/questions/65914250/java-lang-illegalstateexception-you-must-either-set-a-text-or-a-view

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

1 Reply

0 votes
by (71.8m points)
private fun userLogin() {
        val email: String = emailText.text.toString().trim()
        val password: String = passwordText.text.toString().trim()

        val stringRequest = object : StringRequest(
                Method.POST, URL_LOGIN,
                Response.Listener<String> { response ->
                    try {
                        val obj = JSONObject(response)
                        if (!obj.getBoolean("error")) {
                            SharedPrefManager.getInstance(applicationContext)
                                    ?.userLogin(
                                            obj.getInt("id"),
                                            obj.getString("email"))
                            Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG).show()

                        }else{
                            Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG).show()

                        }
                           this.finish()
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                },
                Response.ErrorListener { volleyError -> Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show() }) {
            @Throws(AuthFailureError::class)
            override fun getParams(): Map<String, String> {
                val params = HashMap<String, String>()
                params.put("email", email)
                params.put("password", password)
                return params
            }
        }


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.login_layout)

    buttonLogin.setOnClickListener {
        userLogin()
       
    }
}

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

...