Reputation: 27
I am trying to figure out the best way to handle exceptions with the onClick listeners. Everything in the code works fine, EXCEPT for the fact that if I press +, -, *, /, =
before entering a number or after calculating, it crashes the app. Any help would be appreciated. This code is a fragement of a multi-functional tool application that I am working on. Code below:
**edited to remove code not relevant to error and add in debugging output
public class Calculator extends Fragment {
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = ADDITION;
display.setText(String.valueOf(val1) + " + ");
displayInfo.setText(null);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = SUBTRACTION;
display.setText(String.valueOf(val1) + " - ");
displayInfo.setText(null);
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = MULTIPLICATION;
display.setText(String.valueOf(val1) + " * ");
displayInfo.setText(null);
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = DIVISION;
display.setText(String.valueOf(val1) + " / ");
displayInfo.setText(null);
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = EQUALS;
display.setText(String.valueOf(val1));
val1 = Double.NaN;
val2 = Double.NaN;
displayInfo.setText(null);
}
}
});
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.calculator, container, false);
}
private void setupUIView(){
zero = (Button)getView().findViewById(R.id.button_0);
one = (Button)getView().findViewById(R.id.button_1);
two = (Button)getView().findViewById(R.id.button_2);
three = (Button)getView().findViewById(R.id.button_3);
four = (Button)getView().findViewById(R.id.button_4);
five = (Button)getView().findViewById(R.id.button_5);
six = (Button)getView().findViewById(R.id.button_6);
seven = (Button)getView().findViewById(R.id.button_7);
eight = (Button)getView().findViewById(R.id.button_8);
nine = (Button)getView().findViewById(R.id.button_9);
decimal = (Button)getView().findViewById(R.id.button_dec);
negate = (Button)getView().findViewById(R.id.button_neg);
equal = (Button)getView().findViewById(R.id.button_eql);
multiply = (Button)getView().findViewById(R.id.button_mult);
add = (Button)getView().findViewById(R.id.button_add);
sub = (Button)getView().findViewById(R.id.button_sub);
divide = (Button)getView().findViewById(R.id.button_div);
percent = (Button)getView().findViewById(R.id.button_prcnt);
power = (Button)getView().findViewById(R.id.button_pwr);
clear = (Button)getView().findViewById(R.id.button_clr);
display = (TextView)getView().findViewById(R.id.display);
displayInfo = (TextView)getView().findViewById(R.id.displayinfo);
}
private void compute(){
if(!Double.isNaN(val1)){
val2 = Double.parseDouble(displayInfo.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 EQUALS:
break;
}
}
else{
val1 = Double.parseDouble(displayInfo.getText().toString());
}
}
}
logcat:
Process: myapplication, PID: 29796
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at myapplication.Calculator.compute(Calculator.java:260)
at myapplication.Calculator.access$400(Calculator.java:19)
at myapplication.Calculator$16.onClick(Calculator.java:187)
at android.view.View.performClick(View.java:7870)
at android.widget.TextView.performClick(TextView.java:14952)
at android.view.View.performClickInternal(View.java:7839)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29315)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
Upvotes: 1
Views: 68
Reputation: 118
val1 = Double.parseDouble(displayInfo.getText().toString());
This line should be your first problem. You are trying to set val1, but displayInfo.getText() is returning "" or NULL.
One way to fix your problem is wrapping everything inside of your onclick()-Method with an if-statement checking if the displayInfo is empty. e.g.:
public void onClick(View v) {
if(!(displayInfo.getText().toString().equals(""))){
compute();
ACTION = DIVISION;
display.setText(String.valueOf(val1) + " / ");
displayInfo.setText("");
}
}
Note that I replaced the text-reset of displayInfo with an empty String, since you are trying to concatenate null with some String, which lets your app crash after calculating once.
Upvotes: 2