I am using Android Studio 3.2 and My app is showing up but the buttons are not triggering actions please suggest any code changes so that they would trigger events on the app
The MainActivity code is :
package com.example.myfalconcalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView expression;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button num1 = findViewById(R.id.num1);
expression = findViewById(R.id.expression);
Button num2 = findViewById(R.id.num2);
Button num3 = findViewById(R.id.num3);
Button num4 = findViewById(R.id.num4);
Button num5 = findViewById(R.id.num5);
Button num6 = findViewById(R.id.num6);
Button num7 = findViewById(R.id.num7);
Button num8 = findViewById(R.id.num8);
Button num9 = findViewById(R.id.num9);
Button num0 = findViewById(R.id.num0);
Button plus = findViewById(R.id.plus);
Button minus = findViewById(R.id.minus);
Button equalsTo = findViewById(R.id.equalsTo);
Button closeBrace = findViewById(R.id.closeBrace);
Button openBrace = findViewById(R.id.openBrace);
Button clear = findViewById(R.id.clear);
Button delete = findViewById(R.id.delete);
Button exponential = findViewById(R.id.exponential);
Button divideBy = findViewById(R.id.divideBy);
num1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "1");
}
});
num2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "2");
}
});
num3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "3");
}
});
num4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "4");
}
});
num5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "5");
}
});
num6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "6");
}
});
num7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "7");
}
});
num8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "8");
}
});
num9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "9");
}
});
num0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "0");
}
});
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "+");
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "-");
}
});
exponential.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "*");
}
});
divideBy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "/");
}
});
equalsTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SimpleMathSolver mathSolver = new SimpleMathSolver(expression.getText().toString());
try {
expression.setText(String.valueOf(mathSolver.solve()));
} catch (Exception e) {
expression.setText("Input Not Valid");
}
}
});
closeBrace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + ")");
}
});
openBrace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "(");
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.setText("");
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tempExpression = expression.getText().toString().substring(0, expression.getText().toString().length()-1);
expression.setText(tempExpression);
}
});
}
}
The Main Activity file is depending on another file:
package com.example.myfalconcalculator;
import java.util.Scanner;
import java.util.Stack;
public class SimpleMathSolver {
private String inputExp;
private final char PLUS = '+';
private final char MINUS = '-';
private final char MULTIPLY = '*';
private final char DIVIDE = '/';
private final char OPENBRACE = '(';
private final char CLOSEBRACE = ')';
public SimpleMathSolver(String inputExp) {
this.inputExp = inputExp;
}
public int solve() throws Exception {
String withoutBraceInputExp = solveBraces(this.inputExp);
return solveMath(withoutBraceInputExp);
}
private String solveBraces(String inputExp) throws Exception {
String output = inputExp;
int closeBraceIndex = output.indexOf(CLOSEBRACE);
while (closeBraceIndex >= 0) {
int openBraceIndex = output.substring(0, closeBraceIndex).lastIndexOf(OPENBRACE);
String solveStr = output.substring(openBraceIndex + 1, closeBraceIndex);
int value = solveMath(solveStr);
output = output.substring(0, openBraceIndex) + value
+ output.substring(closeBraceIndex + 1, output.length());
closeBraceIndex = output.indexOf(CLOSEBRACE);
}
return output;
}
private int solveMath(String inputExp) throws Exception {
Stack<Integer> numberStack = new Stack<Integer>();
Stack<Character> symbolStack = new Stack<Character>();
updateStacks(inputExp, numberStack, symbolStack);
solveMathSymbol(numberStack, symbolStack, DIVIDE);
solveMathSymbol(numberStack, symbolStack, MULTIPLY);
solveMathSymbol(numberStack, symbolStack, MINUS);
solveMathSymbol(numberStack, symbolStack, PLUS);
return numberStack.pop();
}
private void solveMathSymbol(Stack<Integer> numberStack, Stack<Character> symbolStack, char symbol) {
Stack<Integer> tempNumberStack = new Stack<Integer>();
Stack<Character> tempSymbolStack = new Stack<Character>();
while (symbolStack.size() > 0) {
char ch = symbolStack.pop();
if (ch == symbol) {
int pop1 = numberStack.pop();
int pop2 = numberStack.pop();
int out = 0;
switch (symbol) {
case PLUS:
out = pop2 + pop1;
break;
case MINUS:
out = pop2 - pop1;
break;
case MULTIPLY:
out = pop2 * pop1;
break;
case DIVIDE:
out = pop2 / pop1;
break;
}
numberStack.push(out);
} else {
tempSymbolStack.push(ch);
int numpop = numberStack.pop();
tempNumberStack.push(numpop);
}
}
while (tempSymbolStack.size() > 0) {
char charpop = tempSymbolStack.pop();
symbolStack.push(charpop);
int numpop = tempNumberStack.pop();
numberStack.push(numpop);
}
}
private void updateStacks(String inputExp, Stack<Integer> numberStack, Stack<Character> symbolStack)
throws Exception {
int number = 0;
for (int i = 0; i < inputExp.length(); i++) {
char ch = inputExp.charAt(i);
if (Character.isDigit(ch)) {
number = number * 10 + Integer.parseInt(String.valueOf(ch));
} else {
numberStack.push(number);
number = 0;
if (ch == PLUS || ch == MINUS || ch == MULTIPLY || ch == DIVIDE) {
symbolStack.push(ch);
} else {
throw new Exception("Unknown Math Symbol " + ch);
}
}
}
numberStack.push(number);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String retryOp = "N";
do {
System.out.println("Enter Math Expression");
String inputExp = scanner.nextLine();
SimpleMathSolver solver = new SimpleMathSolver(inputExp);
try {
System.out.println("Result: " + solver.solve());
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Do you want to try this again");
retryOp = scanner.nextLine();
} while (retryOp.equalsIgnoreCase("Y"));
scanner.close();
System.exit(0);
}
}
this is the activity main.xml file:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/closeBrace"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="1dp"
android:text=")"
app:layout_constraintBottom_toTopOf="@+id/equalsTo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/openBrace"
app:layout_constraintTop_toBottomOf="@+id/divideBy" />
<Button
android:id="@+id/op
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…