If you are working in a non UI thread and you try to display a Toast message, you will get an error message like,
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.(Handler.java:154)
at android.widget.Toast.(ExampleToast.java:78)
at android.widget.Toast.makeText(ExampleToast.java:245)
⛏️ To solve this you need to use runOnUiThread() method
myActivity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(myActivity, "Toast Message Text!", Toast.LENGTH_LONG).show();
}
});
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!