plus 2 number == android studio - java

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.

Related

Display the last operator for an Android calculator in Java

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;
}
});

Android Calculator App forced closed when click the operator's are clicked

I am a beginner app developer and currently I have stumbled on an issue whereby whenever any operator was clicked, the app crashes. If I use a catch exception for the operators, the value will show NaN in the TextView.
Any expertise or feedback is appreciated.
Below is the error from the logcat:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.math.fundamentalmath, PID: 3619
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.math.fundamentalmath.CalculatorFragment.compute(CalculatorFragment.java:62)
at com.math.fundamentalmath.CalculatorFragment.access$000(CalculatorFragment.java:16)
at com.math.fundamentalmath.CalculatorFragment$17.onClick(CalculatorFragment.java:231)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Source Code Below:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
public class CalculatorFragment extends Fragment {
private TextView show,result;
private Button b_square,b_square_root,b_delete,b_reset,b_9,b_8,b_7,b_6,b_5,b_4,b_3,b_2,b_1,b_0,b_dot,b_div,b_mul,b_min,b_plus,b_equal;
private final char ADDITION = '+';
private final char SUBTRACTION = '-';
private final char MULTIPLICATION = '*';
private final char DIVISION = '/';
private final char EQU = '0';
private final char SQR = '²';
private final char SQRT = '√';
private double val1 = Double.NaN;
private double val2;
private char ACTION;
private void compute() {
if (!Double.isNaN(val1)) {
val2 = Double.parseDouble(show.getText().toString());
switch (ACTION){
case ADDITION:
val1 = val1 + val2;
break;
case SUBTRACTION:
val1 = val1 - val2;
break;
case MULTIPLICATION:
val1 = val1 * val2;
break;
case DIVISION:
val1 = val1 / val2;
break;
case SQR:
val1 = Math.pow(val1,2);
break;
case SQRT:
val1 = Math.sqrt(val1);
break;
case EQU:
break;
}
}
else{
val2 = Double.parseDouble(show.getText().toString());
}
}
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_calculator, container, false);
final TextView show = (TextView ) v.findViewById(R.id.show);
final TextView result = (TextView ) v.findViewById(R.id.result);
final Button b_square = (Button) v.findViewById(R.id.b_square);
final Button b_square_root = (Button) v.findViewById(R.id.b_square_root);
final Button b_delete = (Button) v.findViewById(R.id.b_delete);
final Button b_reset = (Button) v.findViewById(R.id.b_reset);
final Button b_9 = (Button) v.findViewById(R.id.b_9);
final Button b_8 = (Button) v.findViewById(R.id.b_8);
final Button b_7 = (Button) v.findViewById(R.id.b_7);
final Button b_6 = (Button) v.findViewById(R.id.b_6);
final Button b_5 = (Button) v.findViewById(R.id.b_5);
final Button b_4 = (Button) v.findViewById(R.id.b_4);
final Button b_3 = (Button) v.findViewById(R.id.b_3);
final Button b_2 = (Button) v.findViewById(R.id.b_2);
final Button b_1 = (Button) v.findViewById(R.id.b_1);
final Button b_0 = (Button) v.findViewById(R.id.b_0);
final Button b_dot = (Button) v.findViewById(R.id.b_dot);
final Button b_div = (Button) v.findViewById(R.id.b_div);
final Button b_mul = (Button) v.findViewById(R.id.b_mul);
final Button b_min = (Button) v.findViewById(R.id.b_min);
final Button b_plus = (Button) v.findViewById(R.id.b_plus);
final Button b_equal = (Button) v.findViewById(R.id.b_equal);
b_square.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
compute();
ACTION = SQR;
result.setText(String.valueOf(val1) + "²");
show.setText(null);
}
});
b_square_root.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
compute();
ACTION = SQRT;
result.setText(String.valueOf(val1) + "√");
show.setText(null);
}
});
b_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(show.getText().length()>0){
CharSequence name = show.getText().toString();
show.setText(name.subSequence(0, name.length()-1));
}
else{
val1 = Double.NaN;
val2 = Double.NaN;
show.setText(null);
result.setText(null);
}
}
});
b_reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction1 = getFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.fragment_container,new CalculatorFragment());
fragmentTransaction1.addToBackStack(null);
fragmentTransaction1.commit();
}
});
b_9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + "9");
}
});
b_8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + "8");
}
});
b_7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + "7");
}
});
b_6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + "6");
}
});
b_5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + "5");
}
});
b_4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + "4");
}
});
b_3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + "3");
}
});
b_2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + "2");
}
});
b_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + "1");
}
});
b_0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + "0");
}
});
b_dot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
show.setText(show.getText().toString() + ".");
}
});
b_div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
compute();
ACTION = DIVISION;
result.setText(String.valueOf(val1) + "/");
show.setText(null);
}
});
b_mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
compute();
ACTION = MULTIPLICATION;
result.setText(String.valueOf(val1) + "X");
show.setText(null);
}
});
b_min.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
compute();
ACTION = SUBTRACTION;
result.setText(String.valueOf(val1) + "-");
show.setText(null);
}
});
b_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
compute();
ACTION = ADDITION;
result.setText(String.valueOf(val1) + "+");
show.setText(null);
}
});
b_equal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
compute();
ACTION = EQU;
result.setText(result.getText().toString() + String.valueOf(val2) + "=");
show.setText(null);
}
});
return v;
}
}
You are defining all your views at the start of the class, then never assigning them. You are creating brand new views inside onCreateView, but trying to access them inside compute(). compute() does not have access to these, it only has access to the (unassigned) class level ones.
Instead of redefining show, you should set a value. So, replace final TextView show = (TextView ) v.findViewById(R.id.show); with show = (TextView) v.findViewById(R.id.show);
I'm not going to comment on the overall code much, but you may want to look into Butterknife. Also, consider whether it is possible to write the same code once, instead of 10 times with a single character changed. DRY.

whenever i perform any operations like add sub equals etc in andriod studio EditText box , my app crashes

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.

Doesn't save the result of previous 2 values if i put 3rd value for addtion

Sir I want to know, how to put here my condition that it must add previous result with current value by pressing Add Button, instead of this it just add current and previous value, please help me
btn_Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText() + "");
Add = true ;
ed1.setText(null);
}
});
#Tim Biegeleisen, #Henry, #NiVeR 54, # Jeeva here has given up my code
what you would suggest?
package com.example.maher.androidspinnertutorial;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class calculatr extends AppCompatActivity {
Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0,btn_Add,btn_Sub,btn_Mul,btn_Div,btn_calc,btn_dec,btn_clear;
EditText ed1;
float Value1, Value2;
boolean mAddition, mSubtract, mMultiplication, mDivision ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculatr);
btn_0 = (Button) findViewById(R.id.btn_0);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_Add = (Button) findViewById(R.id.btn_Add);
btn_Div = (Button) findViewById(R.id.btn_Div);
btn_Sub = (Button) findViewById(R.id.btn_Sub);
btn_Mul = (Button) findViewById(R.id.btn_Mul);
btn_calc = (Button) findViewById(R.id.btn_calc);
btn_dec = (Button) findViewById(R.id.btn_dec);
btn_clear = (Button) findViewById(R.id.btn_clear);
ed1 = (EditText) findViewById(R.id.edText1);
btn_0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"0");
}
});
btn_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"1");
}
});
btn_2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"2");
}
});
btn_3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"3");
}
});
btn_4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"4");
}
});
btn_5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"5");
}
});
btn_6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"6");
}
});
btn_7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"7");
}
});
btn_8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"8");
}
});
btn_9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"9");
}
});
btn_dec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+".");
}
});
btn_Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText() + "");
mAddition = true ;
ed1.setText(null);
}
});
btn_Sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText() + "");
mSubtract = true ;
ed1.setText(null);
}
});
btn_Mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText() + "");
mMultiplication = true ;
ed1.setText(null);
}
});
btn_Div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText()+"");
mDivision = true ;
ed1.setText(null);
}
});
btn_calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value2 = Float.parseFloat(ed1.getText() + "");
if (mAddition == true){
ed1.setText(Value1 + Value2 +"");
mAddition=false;
}
if (mSubtract == true){
ed1.setText(Value1 - Value2 +"");
mSubtract=false;
}
if (mMultiplication == true){
ed1.setText(Value1 * Value2 + "");
mMultiplication=false;
}
if (mDivision == true){
ed1.setText(Value1 / Value2+"");
mDivision=false;
}
}
});
btn_clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText("");
}
});
}
}

how to add java code in fragment activity in android

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.

Categories

Resources