Why my TOAST not appear?

Search This thread

AndreBernard

New member
May 27, 2014
3
0
Hello,

I'm newer in ANDROID and not understand why the TOAST not appear in this case.
If i comment the loop "Boucle()" that's works, else not :(

Code:
package com.example.toast;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Toast toast= Toast.makeText(MainActivity.this, "I'm a TOAST and i not appear.", Toast.LENGTH_LONG);  
		toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
		toast.show();
		
		//Boucle();
	}
	

	public void Boucle() 
	{
		
		Integer a = 0;
		
		for(int i = 1; i <= 500000; i++)
		{
			
			a++;

		}
		
	}

}

Thanks and good day
 

giuliomvr

Senior Member
May 16, 2013
189
235
Cologne
Weird, I copied your code and it worked just fine.
You may try to replace MainActivity.this with this, as classname.this is only needed when working with nested class. And for your information: You can chain the toast call like this:
Code:
Toast.makeText(MainActivity.this, "I'm a TOAST and i not appear.", Toast.LENGTH_LONG).setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0).show();
and methods normally start with a lower case letter.
 
  • Like
Reactions: MattMatt0240

AndreBernard

New member
May 27, 2014
3
0
Thanks for the tips to chain the Toast, i don't know that :cool:
Tomorow i test another time, for see if it's better
Again thanks for your help
 

mohamedrashad

Senior Member
Nov 15, 2012
1,087
618
26
ismailia
Code:
Toast toast= Toast.makeText( getApplicationContext() , "I'm a TOAST and i not appear.", Toast.LENGTH_LONG);  
		toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
		toast.show();

just change the MainActivity.this with getApplicationContext() or getBaseContext() :good:
 

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    Weird, I copied your code and it worked just fine.
    You may try to replace MainActivity.this with this, as classname.this is only needed when working with nested class. And for your information: You can chain the toast call like this:
    Code:
    Toast.makeText(MainActivity.this, "I'm a TOAST and i not appear.", Toast.LENGTH_LONG).setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0).show();
    and methods normally start with a lower case letter.