This Toast was not created with Toast.makeText() : Android RuntimeException


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()