If you have used Toast object in your Android Application project and you receive an RuntimeException error message in LogCat saying,
FATAL EXCEPTION: main java.lang.RuntimeException:
Unable to start activity ComponentInfo
{com.example.toastexample/com.example.toastexample.MainActivity }: java.lang.RuntimeException:
This Toast was not created with Toast.makeText()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
...
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: This Toast was not created with Toast.makeText()
at android.widget.Toast.setText(Toast.java:275)
at com.example.toastexample.MainActivity.onCreate(MainActivity.java:19)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
⚡️ You must to use makeText() method to set text and duration instead of using setText() on Toast object!.
Java Example:Context appContext = getApplicationContext();
CharSequence text = "Android: Hello toast!";
int toastDuration = Toast.LENGTH_SHORT;
Toast myToast = Toast.makeText(appContext, toastMsgText, toastDuration);
myToast.show();
Kotlin Example:
val toastMsgText = "Android: Hello toast!"
val toastDuration = Toast.LENGTH_SHORT
val myToast = Toast.makeText(applicationContext, toastMsgText, toastDuration)
toast.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:
- Installing MongoDB on Linux/Unix/macOS/Ubuntu - MacOS
- How to remove or unstage a file from git staged area - Git
- How to Recover Deleted Messages on iPhone - iOS
- Fix: Jupyter Notebook Black Web Page on Localhost - Python
- Linux Remove or Delete Files and Directories using Terminal Commands - Linux
- Android : Unable to load VM from snapshot : Mac OS X Error - Android
- How to Clear Gradle Cache on Mac - Gradle
- Java + Spring JDBC Template + Gradle Example - Java