Tablayout is properly working with single textview which is displayed in each tab, i add calculator to one of tab named Tabfragment2, calculator xml in Tabfragment2 has no error while java code of calculator in fragment2.java i face error in findViewById all of them
public class TabFragment2 extends Fragment {
Activity referenceActivity;
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btndiv, btnadd, btnsub, btnmul, btnzero, btndot, btnclear, btnequal ;
TextView textview;
float ValueOne, ValueTwo;
boolean Addition, Subtract, Multiplication, Division;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_fragment_2, container, false);
referenceActivity = getActivity();
btnzero = (Button) findViewById(R.id.btnzero);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btnadd = (Button) findViewById(R.id.btnadd);
btndiv = (Button) findViewById(R.id.btndiv);
btnsub = (Button) findViewById(R.id.btnsub);
btnmul = (Button) findViewById(R.id.btnmul);
btndot = (Button) findViewById(R.id.btndot);
btnclear = (Button) findViewById(R.id.btnclear);
btnequal = (Button) findViewById(R.id.btnequal);
textview = (TextView) findViewById(R.id.textview);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "9");
}
});
btnzero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "0");
}
});
btndiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "/");
}
});
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textview == null) {
textview.setText("");
} else {
ValueOne = Float.parseFloat(textview.getText() + "");
Addition = true;
textview.setText(null);
}
}
});
btnsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textview == null) {
textview.setText("");
} else {
ValueOne = Float.parseFloat(textview.getText() + "");
Subtract = true;
textview.setText(null);
}
}
});
btndiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textview == null) {
textview.setText("");
} else {
ValueOne = Float.parseFloat(textview.getText() + "");
Division=true;
textview.setText(null);
}
}
});
btnmul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textview == null) {
textview.setText("");
} else {
ValueOne = Float.parseFloat(textview.getText() + "");
Multiplication = true;
textview.setText(null);
}
}
});
btnequal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ValueTwo = Float.parseFloat(textview.getText() + "");
if (Addition == true) {
textview.setText(ValueOne + ValueTwo + "");
Addition = false;
}
if (Subtract == true) {
textview.setText(ValueOne - ValueTwo + "");
Subtract = false;
}
if (Multiplication == true) {
textview.setText(ValueOne * ValueTwo + "");
Multiplication = false;
}
if (Division == true) {
textview.setText(ValueOne / ValueTwo + "");
Division = false;
}
}
});
btnclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText("");
}
});
btndot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + ".");
}
});
return view;
}
}
Replace your all findViewById by below code.
btnzero = (Button) view.findViewById(R.id.btnzero);
btn1 = (Button) view.findViewById(R.id.btn1);
btn2 = (Button) view.findViewById(R.id.btn2);
btn3 = (Button) view.findViewById(R.id.btn3);
btn4 = (Button) view.findViewById(R.id.btn4);
btn5 = (Button) view.findViewById(R.id.btn5);
btn6 = (Button) view.findViewById(R.id.btn6);
btn7 = (Button) view.findViewById(R.id.btn7);
btn8 = (Button) view.findViewById(R.id.btn8);
btn9 = (Button) view.findViewById(R.id.btn9);
btnadd = (Button) view.findViewById(R.id.btnadd);
btndiv = (Button) view.findViewById(R.id.btndiv);
btnsub = (Button) view.findViewById(R.id.btnsub);
btnmul = (Button) view.findViewById(R.id.btnmul);
btndot = (Button) view.findViewById(R.id.btndot);
btnclear = (Button) view.findViewById(R.id.btnclear);
btnequal = (Button) view.findViewById(R.id.btnequal);
textview = (TextView) view.findViewById(R.id.textview);
call findViewById with your view object.
btnzero = (Button) view.findViewById(R.id.btnzero);
btn1 = (Button) view.findViewById(R.id.btn1);
btn2 = (Button) view.findViewById(R.id.btn2);
and go on.
Related
I'm programming a calculator for Android in Java and there is one last thing to do before my program becomes complete.
For example, if I press in this order :5, +, -, /, *, 5, the operation will be "5*5". Actually when I do that it only remains the first operator. How can I fix it? I can't finish it and its driving me crazy. If someone could help me I would be grateful.
Here is my code, I hope its clean and you will be able to understand it :
package com.example.calcapp_training;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnMinus, btnDiv, btnMult, btnPlus, btnDel, btnEqual, btnPoint, btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
TextView input, result;
TextView error;
ImageView image;
double mValueOne, mValueTwo;
int Calculation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView errorMessage = findViewById(R.id.error);
final TextView welcomeMessage = findViewById(R.id.welcome);
btn0 = (Button) findViewById(R.id.btn0);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btnPlus = (Button) findViewById(R.id.btnPlus );
btnMinus = (Button) findViewById(R.id.btnMinus );
btnMult = (Button) findViewById(R.id.btnMult );
btnDiv = (Button) findViewById(R.id.btnDiv );
btnDel = (Button) findViewById(R.id.btnDel);
btnEqual = (Button) findViewById(R.id.btnEqual);
btnPoint = (Button) findViewById(R.id.btnPoint);
input = (TextView) findViewById(R.id.input);
result = (TextView) findViewById(R.id.result);
error = (TextView) findViewById(R.id.error);
image = (ImageView) findViewById(R.id.image);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "9");
}
});
btn0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "0");
}
});
btnDel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText("");
input.setText("");
}
});
btnPoint.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + ".");
}
});
btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 1;
result.setText(null);
}
}
});
btnMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 2;
result.setText(null);
}
}
});
btnMult.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 3;
result.setText(null);
}
}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 4;
result.setText(null);
}
}
});
btnEqual.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mValueTwo = Double.parseDouble(result.getText() + "");
if (Calculation == 1) {
result.setText(mValueOne + mValueTwo + "");
Calculation = 1;
}
else if (Calculation == 2) {
result.setText(mValueOne - mValueTwo + "");
Calculation = 2;
}
else if (Calculation == 3) {
result.setText(mValueOne * mValueTwo + "");
Calculation = 3;
}
else if (Calculation == 4) {
double a = Math.round(mValueOne / mValueTwo);
result.setText(a + "" );
if(mValueTwo == 0) {
errorMessage.setVisibility(View.VISIBLE);
result.setText(mValueOne / mValueTwo + "");
Calculation = 4;
} else {
errorMessage.setVisibility(View.INVISIBLE);
}
Calculation = 4;
}
}
});
}
}
Put all operation button click Calculation outside condition like this:
btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
result.setText(null);
}
Calculation = 1;
}
});
I have been trying to make a simple calculator on Android studio. It shows the calculations perfectly fine. but whenever I try to press equals or plus sub or minus operation when the EditText box is empty, my app always crashes.
I tried different ways but it didn't help.
Is there any way that I can do it and stop my app from crashing?
Here's my java code
package com.example.mathcalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button0,button1,button2,button3,button4,button5,button6,button7,button8,button9,buttonClear,buttonMul,buttonSub,buttonAdd,buttonDec,buttonEqual,buttonDiv;
float val1,val2;
EditText result;
boolean mAdditon,msubtraction,mMultiplication, mDivision;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonMul = (Button) findViewById(R.id.buttonMul);
buttonDiv = (Button) findViewById(R.id.buttonDiv);
buttonClear = (Button) findViewById(R.id.buttonClear);
buttonEqual = (Button) findViewById(R.id.buttonEqual);
buttonDec = (Button) findViewById(R.id.buttonDec);
buttonSub = (Button) findViewById(R.id.buttonSub);
result = (EditText) findViewById(R.id.result);
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"9");
}
});
buttonDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+".");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (result == null)
{
result.setText("");
}
else
{
val1 =Float.parseFloat(result.getText()+"");
mAdditon=true;
result.setText(null);
}
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
msubtraction=true;
result.setText(null);
}
}
});
buttonDiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
mDivision=true;
result.setText(null);
}
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
mMultiplication = true;
result.setText(null);
}
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText("");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
val2 = Float.parseFloat(result.getText() + "");
if (mAdditon == true) {
result.setText(val1 + val2 + "");
mAdditon = false;
}
if (msubtraction == true) {
result.setText(val1 - val2 + "");
msubtraction = false;
}
if (mMultiplication == true) {
result.setText(val1 * val2 + "");
msubtraction = false;
}
if (mDivision == true) {
if (val2 == 0) {
result.setText("Can't Divide by Zero");
return;
} else {
result.setText(val1 / val2 + "");
msubtraction = false;
}
}
}
});
}
}
You say it crashes when the EditText box is empty, so most likely, what is happening is that when you do
Float.parseFloat(result.getText() + "");
result.getText() returns "", or you end up doing
Float.parseFloat("");
which result in java.lang.NumberFormatException: empty String
Try replacing
val1=Float.parseFloat(result.getText()+"");
with
val1 = 0;
if(!result.getText().isEmpty())
val1 = Float.parseFloat(result.getText());
NOTE:
Also, as you use it several times, you can make a method for it
private float getResult() {
int result = 0;
if(!result.getText().isEmpty())
val1=Float.parseFloat(result.getText());
return result;
}
and then replace everywhere with val1 = getResult();
as above code when get data from EditText use edit.getText().toString()
and for Parsing it always wrap it with try/catch block as EditText it is initially empty
some hints about the code
- here if (result == null) result.setText(""); result is always not null.
so for better solution always use try/catch when parsing EditText.
i am developing android simple calculator it works fine but only one issue when i press 2+3 and press = and again = and again = it should result 11 but not worked! my activity file is:
visit https://www.mediafire.com/edit/nit258ur24218e1/MainActivity.java
visit http://uplod.ir/n53dikr3y10w/MainActivity.java.htm
I can't add code
error
please help me
` package com.example.administrator.mashinhesab;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
Button button0, button1, button2, button3, button4, button5, button6,
button7, button8, button9, buttonAdd, buttonSub, buttonDivision,
buttonMul, buttonC, buttonEqual;
EditText edt1;
TextView edt2;
private static final char ADDITION = '+';
private static final char SUBTRACTION = '-';
private static final char MULTIPLICATION = '*';
private static final char DIVISION = '/';
private char CURRENT_ACTION;
private double num1 = Double.NaN;
private double num2;
private DecimalFormat decimalFormat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
Button();
decimalFormat = new DecimalFormat("#.##########");
}
private void Button(){
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "9");
}
});
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "0");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = ADDITION;
edt2.setText(decimalFormat.format(num1) + "+");
edt1.setText(null);
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
edt2.setText(decimalFormat.format(num1) + "-");
edt1.setText(null);
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
edt2.setText(decimalFormat.format(num1) + "*");
edt1.setText(null);
}
});
buttonDivision.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = DIVISION;
edt2.setText(decimalFormat.format(num1) + "/");
edt1.setText(null);
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
edt2.setText(edt2.getText().toString() + decimalFormat.format(num2) + " = " + decimalFormat.format(num1));
num1 = Double.NaN;
CURRENT_ACTION = '0';
}
});
buttonC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(edt1.getText().length() > 0) {
CharSequence currentText =edt1.getText();
edt1.setText(currentText.subSequence(0, currentText.length()-1));
}
else {
num1 = Double.NaN;
num2 = Double.NaN;
edt1.setText("");
edt2.setText("");
}
}
}
);
}
private void computeCalculation () {
if (!Double.isNaN(num1)) {
num2 = Double.parseDouble(edt1.getText().toString());
edt1.setText(null);
if (CURRENT_ACTION == ADDITION)
num1 = this.num1 + num2;
else if (CURRENT_ACTION == SUBTRACTION)
num1 = this.num1 - num2;
else if (CURRENT_ACTION == MULTIPLICATION)
num1 = this.num1 * num2;
else if (CURRENT_ACTION == DIVISION)
num1 = this.num1 / num2;
} else {
try {
num1 = Double.parseDouble(edt1.getText().toString());
} catch (Exception e) {
}
}
}
private void findViews () {
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonAdd = (Button) findViewById(R.id.buttonadd);
buttonSub = (Button) findViewById(R.id.buttonsub);
buttonMul = (Button) findViewById(R.id.buttonmul);
buttonDivision = (Button) findViewById(R.id.buttondiv);
buttonC = (Button) findViewById(R.id.buttonC);
buttonEqual = (Button) findViewById(R.id.buttoneql);
edt1 = (EditText) findViewById(R.id.edt1);
edt2 = (TextView) findViewById(R.id.edt2);
}
}`
enter image description here
I write 6+8=14 and again =write 148 and again =NaN8 and again = NaN
When you handle the equals press you set the CURRENT_ACTION = '0'; so when you press it again none of the conditions are true. What you want to achieve could work if you just remove that line. I also recommend you look into some java and android reference materials so you can improve your code.
I am making a mobile calculator and I am aware there are many that have been created. I tried using others as an example to build mine but I have been stuck for days. I was able to display numbers on the EditText when user selects random numbers, but I cannot figure how to add, sub, multi, divide, the numbers that were selected by the user. Please show an example of how I can solve it. This is my first mobile application.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView editText = (EditText) findViewById(R.id.edit_text);
final Button Button0 = (Button) findViewById(R.id.Button_0);
final Button Button1 = (Button) findViewById(R.id.Button_1);
Button Button2 = (Button) findViewById(R.id.Button_2);
Button Button3 = (Button) findViewById(R.id.Button_3);
Button Button4 = (Button) findViewById(R.id.Button_4);
Button Button5 = (Button) findViewById(R.id.Button_5);
Button Button6 = (Button) findViewById(R.id.Button_6);
Button Button7 = (Button) findViewById(R.id.Button_7);
Button Button8 = (Button) findViewById(R.id.Button_8);
Button Button9 = (Button) findViewById(R.id.Button_9);
Button clear_Btn = (Button) findViewById(R.id.C_Button);
Button decimal_Btn = (Button) findViewById(R.id.Decimal_Button);
Button equal_Btn = (Button) findViewById(R.id.equal_Button);
final Button add_Btn = (Button) findViewById(R.id.Addition_Button);
/////////////////// NUMBER BUTTONS ///////////////////////
Button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 0);
}
});
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editText.setText(editText.getText().toString() + 1);
}
});
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 2);
}
});
Button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 3);
}
});
Button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 4);
}
});
Button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 5);
}
});
Button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 6);
}
});
Button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 7);
}
});
Button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 8);
}
});
Button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 9);
}
});
///////////////////// Sign Buttons /////////////////////
// Clear Button
clear_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText("");
}
});
// Decimal Button
decimal_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + ".");
}
});
// Equal button
equal_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
// Add button
add_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + "+");
}
});
}
}
A couple ways to achieve this, you just need a way to retrieve the current number in the calculator and perform an operation against the new selected number
So store the main number in its own variable, and then perform the desired operation against a new number, before you set the new edittext value
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have the basic numbers working 0-9 and the code i have is
public class MainActivity extends AppCompatActivity {
Button button0 , button1 , button2 , button3 , button4 , button5 , button6 ,
button7 , button8 , button9 , buttonAdd , buttonSub , buttonDivision,
buttonMul , button10 , buttonC , buttonEqual ;
TextView txtView ;
float num1 = 0;
float num2 = 0;
float sum = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
button10 = (Button) findViewById(R.id.button10);
buttonAdd = (Button) findViewById(R.id.buttonadd);
buttonSub = (Button) findViewById(R.id.buttonsub);
buttonMul = (Button) findViewById(R.id.buttonmul);
buttonDivision = (Button) findViewById(R.id.buttondiv);
buttonC = (Button) findViewById(R.id.buttonC);
buttonEqual = (Button) findViewById(R.id.buttoneql);
txtView = (TextView) findViewById(R.id.txtView);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText(txt.getText()+"1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"9");
}
});
However ever when i try to do my add function the app crashes. It seems like a simple thing but other examples i've seen are more complex. My addition code is...
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1 = Float.parseFloat(txtView.getText().toString());
num2 = Float.parseFloat(txtView.getText().toString());
sum = num1 + num2;
}
});
And then when the equal button is pushed my code is as followed. This is the part that is not working for me.
buttonEqual.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText(Float.toString(sum));
}
});
And this causes the app to crash. Any help? Thanks
I think in your buttonAdd function is a mistake.
num1 = Float.parseFloat(txtView.getText().toString());
num2 = Float.parseFloat(txtView.getText().toString());
While you execute your code, num1 and num2 are equal.
I dont understand your error.
but there is no problem, just you must change btnAdd and btnEqual
Can you try this?
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1 = Float.parseFloat(txtView.getText().toString());
txtView.setText("");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num2 = Float.parseFloat(txtView.getText().toString());
sum = num1 + num2;
txtView.setText(Float.toString(sum));
}
});