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();
}
});
More Posts related to Android,
- Check Internet Connection WIFI 4G is active on Android Programmatically
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac]
- How to Change Android Toast Position?
- Fail to connect to camera service Android java RuntimeException
- How to create Custom RatingBar Android Programming Tutorial
- Fixing Android unknown error 961 while downloading app
- Android AlertDialog with Yes No and Cancel Button
- Share or Send SMS via Android Intent
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Pass data between two Android Activities and access it using Intent
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- Android EditText Cursor Colour appears to be white
- Android Development - How to switch between two Activities
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Android : Remove ListView Separator/divider programmatically or using xml property
- Android is starting optimizing... app 1 of 1
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- AlertDialog with single button example : Android
- Android : Exception raised during rendering: action_bar API 22
- Maven : java.lang.ClassNotFoundException: Xmx512m
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Center align text in TextView Android Programming
- How to Download and Install Android adb Tool on Linux, Mac or Windows
- Multiline EditText in Android Example
More Posts:
- How to Open Finder using Mac Terminal - MacOS
- Android Studio emulator/Device logCat logs not displayed - Android-Studio
- How to embed web page in SharePoint - SharePoint
- How to know the Safari Version on Mac - MacOS
- Convert Java Array to ArrayList Code Example - Java
- How to show End of Line Characters in File using Notepad++ - NotepadPlusPlus
- The selected device is incompatible : Android Studio - Android-Studio
- Bash Command to Find String in a File - Bash