gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:42:19 65 4
gpt4 key购买 nike

我是 android 新手,在单击处理数据库请求的按钮时不断收到此错误。
我不知道发生了什么,因为昨天一切正常,而我根本没有做任何更改。
错误日志:

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)
代码错误日志指的是:
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
}
}
调用此函数的代码:
 override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.login_layout)

buttonLogin.setOnClickListener {
userLogin()
this.finish()
}
}
老实说,我不知道这个错误是什么意思,如果我需要提供更多代码,请告诉我。

最佳答案

在 android 11(API 级别 30)Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)将在 Toast 中返回 true类(class) show()方法,对于 android 11,如 android developer site 中所述
现在,如果您想跳过此异常,请不要通过 null在 Toast 的消息参数中 Toast.makeText(applicationContext, obj.getString("message"),Toast.LENGTH_LONG).show()在这里你的obj.getString("message")在某些情况下为 null,因此您检查 null 然后显示 Toast喜欢以下

if(obj!=null && obj.getString("message")!=null){
Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG).show()
}else//static message
像这样我们可以防止异常

关于java.lang.IllegalStateException : You must either set a text or a view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65914250/

65 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com