Displaying Integer on Toast

Displaying integer on toast

Toast.makeText either takes a CharSequence or an int as its second argument.

However, the int represents a resource ID (such as R.string.hello_world).

The application crashes probably because no resource is found with that ID, since it's not an ID to start with, but an arbitrary integer.

In your case, use Toast.makeText(this,String.valueOf(bignum),Toast.LENGTH_LONG).show();.

Displaying integer values in toast message

you have number format exception as you are getting string from edit text without performing any action as the result of which it is crashing ..\
do it like that :

public class MainActivity extends Activity {
double bmi;
Button calculate ;
EditText e1,e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText3);
Button calculate = (Button)findViewById(R.id.button1);
calculate.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
double d1 = Double.parseDouble(e1.getText().toString());
double d2 = Double.parseDouble(e2.getText().toString());
bmi = ( d2 / (d1*d1));
Toast.makeText(getApplicationContext(), "Your BMI is " + bmi, Toast.LENGTH_SHORT).show();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Why does this print 0 in toast message?

There was a casting issue. When you trying to do it all using integer it only converts to integer numbers. In the middle of this arithmetic operation, there was a middle state like 0.93 *100 but it is not the way when only integers are there. That 0.93 is rounded to 0 then it will multiply by 100 that's why this happened. Use below code to avoid that casting error. It implicitly bind with doubles

Toast.makeText(this, "" + 109.0 / (7.0 + 109.0)*100 , Toast.LENGTH_SHORT).show();

App crashes when displaying casted int with Toast

  Use the following way to show toast 
Toast.makeText(ListView_Confirmation.this, ""+code, Toast.LENGTH_LONG).show();

EditText check if input is from 0 to 9 and display toast?

Try this code:

try {
int number = Integer.parseInt(letter);
if (number > 9 || number < 0) {
Toast.makeText(this, "Number should be from 0 to 9",Toast.LENGTH_SHORT).show();
return;
}

// number is correct, work with your number here

} catch (NumberFormatException nfe) {
Toast.makeText(this, "Unable to convert the string to number: " + letter,Toast.LENGTH_SHORT).show();
return;
}


Related Topics



Leave a reply



Submit