Reputation:
Here's how I implement Toast:
if ((name == "") || (pass == "")){
Toast invalidLoginToast = new Toast.makeText(this, "aaa", 3).show();
} else {
Intent intent = new Intent (this, AnnouncementsActivity.class);
String deviceId = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
intent.putExtra("NAME", name);
intent.putExtra("ID", deviceId);
startActivity(intent);
}
But I get the error
Error:(34, 48) error: cannot find symbol class makeText
Neccessary imports are already made. Am I sending wrong parameters into the method?
Upvotes: 2
Views: 1074
Reputation: 1015
Toast invalidLoginToast = new Toast.makeText(this, "aaa", 3).show();
just remove the part (Toast invalidLoginToast = new)
from your coding as you don't need to create object to Toast class to access makeText function, because its static it can be accessed directly using class name itself as Toast.makeText
just write it as
Toast.makeText(this, "aaa", 3).show();
(or)
Toast.makeText(this,"aaa",Toast.LENGTH_SHORT).show();
Upvotes: 0
Reputation: 17131
Toast.makeText(context, text, duration);
context
getApplicationContext() - Returns the context for all activities running in application.
getBaseContext() - If you want to access Context from another context within application you can access.
getContext() - Returns the context view only current running activity.
text
text - Return "STRING" , If not string you can use type cast. (string)num // type caste duration
duration
Toast.LENGTH_SHORT - Toast delay 2000 ms predefined
Toast.LENGTH_LONG - Toast delay 3500 ms predefined
milisecond - Toast delay user defined miliseconds (eg. 4000)
if ((name.equals("")) || (pass.equals(""))){
Toast.makeText(this,"aaa",Toast.LENGTH_SHORT,).show();
}
Upvotes: 0
Reputation: 75788
Instantiate a Toast object with one of the makeText() methods. This method takes three parameters: the application Context, the text message, and the duration for the toast. It returns a properly initialized Toast object. You can display the toast notification with show();
Toast.makeText(context, text, duration)
Read official guideline about Toast .
Toast.makeText(CurrentActivityName.this,"aaa",Toast.LENGTH_SHORT).show();
Upvotes: 3
Reputation: 1149
Remove new keyword in front of Toast initialization. use
Toast invalidLoginToast = Toast.makeText(this, "aaa", 3).show();
You don't need to use new
keyword while creating Toast as it is provided by factory method inside Toast which does that for you.
Instantiate a Toast object with one of the makeText() methods. This method takes three parameters: the application Context, the text message, and the duration for the toast. It returns a properly initialized Toast object. You can display the toast notification with show()
Upvotes: 0
Reputation: 4620
This line is wrong,
Toast invalidLoginToast = new Toast.makeText(this, "aaa", 3).show();
Replace it with
Toast.makeText(this, "aaa", 3).show();
Toast is having makeText
method as static, and you can call it from the class name, don't call it the way you are calling it(i.e by new operator).
For further info, please visit the official documentation
Upvotes: 0