IllegalStateException in Android

Jyotishgher Astrology
By -
0

 

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

An IllegalStateException is a runtime exception in Java that is thrown to indicate that a method has been invoked at the wrong time. This exception is used to signal that a method is called at an illegal or inappropriate time.


IllegalStateException in Android using Java

IllegalStateExceptionis the sub-class of RuntimeException class, and therefore it is an unchecked exception. It is raised by the programmer or by the API developer explicitly. It is thrown when a method call illegal or a method is called at incorrect time.

For example, once we start a thread, we cannot restart the same thread again; if we try to do that, it throws a runtime exception i.e., IllegalStateException.

The errorlog:
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)

                               

in android 11(API level 30) Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM) will return true in Toast class show() method, for android 11 as mention in android developer site

for now if you want to skip this exception don't pass null in message parameter of Toast Toast.makeText(applicationContext, obj.getString("message"),Toast.LENGTH_LONG).show() here your obj.getString("message") getting null for certain cases so you check null then show Toast like following

if(obj!=null && obj.getString("message")!=null){
Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG).show()
}else//static message

like this we can prevent exception


Conclusions:

The exception which is checked by the compiler for the smooth execution of the program at runtime is called a checked exception. In our program, if there is a chance of rising checked exception then compulsory we should handle that checked exception (either by try-catch or throws keyword) otherwise we will get compile-time error.

Examples of checked exceptions are ClassNotFoundException, IOException, SQLException, etc.

Tags:

Post a Comment

0Comments

Post a Comment (0)