java.lang.RuntimeException: Unable to start activity ComponentInfo { com.project/com.project.simple} : java.lang.NumberFormatException
EditText et1,et2,et3;
Button b1, b2;
Float two,three,four;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.can);
et1 = (EditText) findViewById(R.id.editText1);
two = Float.valueOf(et1.getText().toString());
et2 = (EditText) findViewById(R.id.editText2);
three = Float.valueOf(et2.getText().toString());
et3 = (EditText) findViewById(R.id.editText3);
four = Float.valueOf(et3.getText().toString());
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
you are in onCreate() where the fields are specified. the user could not have time to enter any valid data. you need to move the fetching of the data somewhere else...like your onClick() perhaps.
move your code from onCerate to an other method
(for example the onClick method of the Button )
and you should
try this:
try{
two = Float.parseFloat(et1.getText().toString());
}catch(NumberFormatException e){
two = 0;
Toast toast = Toast.makeText(this, 'Invalid number format on `two` field', 500);
toast.show();
}
For each text fields what you want to read
Comment:
float format is 2.3 and don't use 2,3
It seems that the input is not valid. Please check twice that you don't try to parse a letters to a number. If you have a float please check that you use the right locale for parsing. E.g. in germany is pi 3,1415... and not 3.1415...
If you cannot preparse the values you could put the parsing trys in try catch blocks like this:
float value;
try {
value=Float.parseFloat(someString);
} catch(NumberFormatException e) {
// input was no valid float
}
Related
I'm trying to build basic calculator, but when I put a number at first edittext and hit add button, it gets crashed.
It is fine when I add two number in both editTexts. There was no problem in that. But the problem happened when I put only one number and hit add.
It is throwing NumberFormatException: Invalid int: "".
here is my basic code.
public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add,sub,multi,div;
TextView MyResults;
String Number1 ;//;
String Number2 ;//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText)findViewById(R.id.num1);
num2 = (EditText)findViewById(R.id.num2);
add = (Button)findViewById(R.id.Add);
sub = (Button)findViewById(R.id.Sub);
multi = (Button)findViewById(R.id.Multiple);
div = (Button)findViewById(R.id.Divide);
MyResults = (TextView)findViewById(R.id.results);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Number1 =num1.getText().toString();
Number2 =num2.getText().toString();
int mAdd1 = Integer.valueOf(Number1);
int mAdd2 = Integer.valueOf(Number2);
int myAdd = mAdd1+mAdd2;
MyResults.setText(String.valueOf(myAdd));
Toast.makeText(getApplicationContext(), "Added: "+ myAdd, Toast.LENGTH_LONG).show();
}
});
}//end OnCreate.
When you leave an EditText empty
int mAdd2 = Integer.valueOf(Number2);
Will crash because a empty String is not a valid Integer (hence the NumberFormatException).
You could put it in a try/catch like this:
try {
Number1 =num1.getText().toString();
Number2 =num2.getText().toString();
int mAdd1 = Integer.valueOf(Number1);
int mAdd2 = Integer.valueOf(Number2);
MyResults.setText(String.valueOf(myAdd));
Toast.makeText(getApplicationContext(), "Added: "+ myAdd, Toast.LENGTH_LONG).show();
catch (NumberFormatException e) {
//Give a toast saying the user did not enter two correct values
}
Or alternatively, check the value beforehand and check if nothing was entered:
number1 =num1.getText().toString();
number2 =num2.getText().toString();
if (number1.equals("") || number2.equals("")) {
//Show error or set numbers to a different value
}
However, for this to work you must assume the user did not enter anything that isn't a valid Integer like a letter, for example by setting the EditText's input type to numbers.
android:inputType="number"
You should do validation checking for a blank edit text and not do the calculation in this case.
Instead you might take advantage of EditText's setError(CharSequence) method where you can show a message to the user why the input is not valid.
I have 2 TextView, and 1 Button. I want to be able to click the button and to save the text on the first TextView and show the value on the second TextView.
I've tried this code:
public void buttonOnClick(){
Button submit = (Button) findViewById(R.id.submit);
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.outputText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textout.setText(editName.getText()+"\n"+editEmail.getText());
}
});
}
But I get an error on 'textout'. When I clock the red light buble, it says 'create a local variable', field text'.
Try this
public void onClick(View v) {
textout.setText(editName.getText().toString()+"\n"+editEmail.getText().toString());
}
Your problem stimulates from the fact that EditText getText() returns Editable Object. It's not a String and you can't concat 2 Editable Objects.
You have 2 Options:
textout.setText(editName.getText().toString() + "\n" + editEmail.getText().toString());
And Second you can use SpanableStringBuilder
SpannableStringBuilder sb = new SpannableStringBuilder(editName.getText());
sb.append("\n").append(editEmail.getText());
The Second options allows you also to decorate the Text and save you the Need to build a String which is (maybe) better.
public class MainActivity extends Activity {
EditText number1, number2;
String number_1, number_2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
After running the application, why does number_1 have an empty value.
onCreate method is create view.
So EditText number1, nubmer2 is not create yet.
If you want get number1 or number2's value, try in onPause().
Try this..
You have to get the text in like OnClick while starting application both EditText is empty. So that it returns empty.
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
Make sure you have predefined the value in the edittext and that should work
i need to see your xml.... if your trying to get numbers you have to parse the string to an int anyway... what are you trying to achieve? you simply want the text, from each of the textviews? is there text set in the xml?
Make sure you have predefined the value in the edittext in xml file.
like in your layout file android:text="your string" or else you have to set string after binding
oncreate method like.
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
number1.setText("your string");
then you have to get string from below code.
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
You need to fill the EditTexts with something before you can get their value. So create a button, and after filling the EditTexts, then get their values by clicking the button.
Perhaps like this:
public class MainActivity extends Activity {
EditText number1, number2;
String number_1, number_2;
Button submit;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
submit = (Button)findViewById(R.id.btn1); //Define a button in your layout xml file with the ID field set as "btn1"
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
}
});
i try to make Multiple-calc operation in one click but when i click on button done its show error so how can i modification this code to be able to calc all of this operations in one click with out any errors . here is the code
public class calcit extends Activity {
EditText onehighx,twowightx,threefinallresultx,four100x,fiveresultof100x,sixresultofresultx;
Button donee;
BigDecimal onehigh,twowight,threefinallresult,four100,fiveresultof100,sixresultofresult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calcit);
donee = (Button) findViewById(R.id.button1);
onehighx = (EditText) findViewById(R.id.high);// high
twowightx = (EditText) findViewById(R.id.Weight);//Weight
threefinallresultx = (EditText) findViewById(R.id.finall_result);//result =Weight * result of result 100
four100x = (EditText) findViewById(R.id.e100);//100
fiveresultof100x = (EditText) findViewById(R.id.re_of_100);// result of 100 = high * 100
sixresultofresultx = (EditText) findViewById(R.id.result_of_100_100);// result of result 100 /ex : 1.75*1.75
donee.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onehigh = new BigDecimal(onehighx.getText().toString());
twowight= new BigDecimal(twowightx.getText().toString());
threefinallresult = new BigDecimal(threefinallresultx.getText().toString());
four100 = new BigDecimal(four100x.getText().toString());
fiveresultof100 = new BigDecimal(fiveresultof100x.getText().toString());
sixresultofresult = new BigDecimal(sixresultofresultx.getText().toString());
//---here i want to fix this code to be able to show all of this Multiple-calc operations in
//(threefinallresultx)------
fiveresultof100x.setText((onehigh).divide(four100).toString());
sixresultofresultx.setText((fiveresultof100).multiply(fiveresultof100).toString());
threefinallresultx.setText((twowight).divide(sixresultofresult).toString());
}
});
}
If I am not wrong you want to do calculations on values fetched from Edittexts but you are instead trying to divide or multiply setText boxes and not the values contained in them. So just fetch values in appropriate format and calculate them normally such as
onehigh=onehigh/four100;
fiveresultof100=fiveresultof100*fiveresultof100;
twowight=twowight/sixresultofresult;
and then set them in textboxes
fiveresultof100x.setText(onehigh);
ixresultofresultx.setText(fiveresultof100);
threefinallresultx.setText(twowight);
Beginning I should say that I am very new to programming.
I am building an android application which includes a calculator function. What I want to do is on button click to get the user input from two EditTexts, add them together and then display the result in a TextView. Similar questions have been covered by others, but what they don't cover is how do you actually display the result in a TextView (or at least I didn't find one myself). So I tried the following which was suggested in many posts:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.team2);
//finds the references for the view in the layouts
b_t2p_ok = (Button) findViewById(R.id.bOK2);
EditText1 = (EditText) findViewById(R.id.etPaixnidi2);
EditText2 = (EditText) findViewById(R.id.etPontoi2);
b_t2p_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//First I am trying to do this for one EditText
String myEditValue = etPaixnidi20.getText().toString();
int myEditNum = Integer.parseInt(myEditValue);
myEditNum.setText(textOut);
}
});
}
However when I try to display the int myEditNum in a TextView(textOut1) using the setText() method I get an error for the setText method (saying: ''Cannot invoke setText(TextView) on the primitive type int'').
I also tried (below) to convert int myEditNum to a String and then display it to a TextView but still doesn't work as I get the folloing error for the setText method: ''The method setText(TextView) is undefined for the type String''.
EditText myEdit = (EditText) findViewById(R.id.edittext1);
String myEditValue = myEdit.getText().toString();
int myEditNum = Integer.parseInt(myEditValue);
String texting = Integer.toString(myEditNum);
texting.setText(textOut1);
Why does this happen and how do I fix this?
You need to call setText(string) on a Textview. So you can do something like:
// Replace R.id.textview1 with the id of your textview
TextView tv = (TextView) findViewById(R.id.textview1);
tv.setText(myEditValue);
You seem to be using the wrong variable to settext here. the View class can do setText()
Try:
...
public void onClick(View v)
{
//First I am trying to do this for one EditText
String myEditValue = etPaixnidi20.getText().toString();
EditText1.setText( myEditValue );
// or
EditText2.setText( myEditValue );
}
...
you should use
textOut1.setText(texting)
You have the TextView and the String the wrong way round.
textOut1.setText(texting);