My android studio made apk is not working properly - java

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/openBrace"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginBottom="1dp"
android:text="("
app:layout_constraintBottom_toTopOf="#+id/equalsTo"
app:layout_constraintEnd_toStartOf="#+id/closeBrace"
app:layout_constraintStart_toEndOf="#+id/num9"
app:layout_constraintTop_toBottomOf="#+id/exponential" />
<Button
android:id="#+id/divideBy"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="/"
app:layout_constraintBottom_toTopOf="#+id/closeBrace"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/exponential"
app:layout_constraintTop_toBottomOf="#+id/minus" />
<Button
android:id="#+id/exponential"
android:layout_width="74dp"
android:layout_height="76dp"
android:text="*"
app:layout_constraintBottom_toTopOf="#+id/openBrace"
app:layout_constraintEnd_toStartOf="#+id/divideBy"
app:layout_constraintStart_toEndOf="#+id/num6"
app:layout_constraintTop_toBottomOf="#+id/plus" />
<Button
android:id="#+id/plus"
android:layout_width="74dp"
android:layout_height="76dp"
android:text="+"
app:layout_constraintBottom_toTopOf="#+id/exponential"
app:layout_constraintEnd_toStartOf="#+id/minus"
app:layout_constraintStart_toEndOf="#+id/num3"
app:layout_constraintTop_toBottomOf="#+id/delete" />
<Button
android:id="#+id/minus"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="-"
app:layout_constraintBottom_toTopOf="#+id/divideBy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/plus"
app:layout_constraintTop_toBottomOf="#+id/delete" />
<Button
android:id="#+id/num0"
android:layout_width="252dp"
android:layout_height="76dp"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="5dp"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/equalsTo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num8" />
<Button
android:id="#+id/num5"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginBottom="2dp"
android:text="5"
app:layout_constraintBottom_toTopOf="#+id/num8"
app:layout_constraintEnd_toStartOf="#+id/num6"
app:layout_constraintStart_toEndOf="#+id/num4"
app:layout_constraintTop_toBottomOf="#+id/num2" />
<Button
android:id="#+id/num4"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="1dp"
android:text="4"
app:layout_constraintBottom_toTopOf="#+id/num7"
app:layout_constraintEnd_toStartOf="#+id/num5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num1" />
<Button
android:id="#+id/num6"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="2dp"
android:text="6"
app:layout_constraintBottom_toTopOf="#+id/num9"
app:layout_constraintEnd_toStartOf="#+id/exponential"
app:layout_constraintStart_toEndOf="#+id/num5"
app:layout_constraintTop_toBottomOf="#+id/num3" />
<Button
android:id="#+id/num8"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginTop="1dp"
android:text="8"
app:layout_constraintBottom_toTopOf="#+id/num0"
app:layout_constraintEnd_toStartOf="#+id/num9"
app:layout_constraintStart_toEndOf="#+id/num7"
app:layout_constraintTop_toBottomOf="#+id/num5" />
<Button
android:id="#+id/num7"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="1dp"
android:text="7"
app:layout_constraintBottom_toTopOf="#+id/num0"
app:layout_constraintEnd_toStartOf="#+id/num8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num4" />
<Button
android:id="#+id/num9"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:text="9"
app:layout_constraintBottom_toTopOf="#+id/num0"
app:layout_constraintEnd_toStartOf="#+id/openBrace"
app:layout_constraintStart_toEndOf="#+id/num8"
app:layout_constraintTop_toBottomOf="#+id/num6" />
<Button
android:id="#+id/num3"
android:layout_width="84dp"
android:layout_height="76dp"
android:text="3"
app:layout_constraintBottom_toTopOf="#+id/num6"
app:layout_constraintEnd_toStartOf="#+id/plus"
app:layout_constraintStart_toEndOf="#+id/num2"
app:layout_constraintTop_toBottomOf="#+id/clear" />
<Button
android:id="#+id/num2"
android:layout_width="84dp"
android:layout_height="76dp"
android:text="2"
app:layout_constraintBottom_toTopOf="#+id/num5"
app:layout_constraintEnd_toStartOf="#+id/num3"
app:layout_constraintStart_toEndOf="#+id/num1"
app:layout_constraintTop_toBottomOf="#+id/clear" />
<Button
android:id="#+id/equalsTo"
android:layout_width="150dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="5dp"
android:text="="
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/num0"
app:layout_constraintTop_toBottomOf="#+id/num9" />
<Button
android:id="#+id/clear"
android:layout_width="252dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="8dp"
android:text="Clear"
app:layout_constraintEnd_toStartOf="#+id/delete"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/expression" />
<TextView
android:id="#+id/expression"
android:layout_width="400dp"
android:layout_height="331dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="8dp"
android:text=""
app:layout_constraintBottom_toTopOf="#+id/clear"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/delete"
android:layout_width="148dp"
android:layout_height="76dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="Delete"
app:layout_constraintBottom_toTopOf="#+id/plus"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/clear"
app:layout_constraintTop_toBottomOf="#+id/expression" />
<Button
android:id="#+id/num1"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:text="1"
app:layout_constraintBottom_toTopOf="#+id/num4"
app:layout_constraintEnd_toStartOf="#+id/num2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clear" />
</android.support.constraint.ConstraintLayout>
please suggest how to solve this problem. The

you can't do final Button num1 = findViewById(R.id.num1); outside of oncreate, all your findViewById(R.id.foo); statements have to be inside onCreate (or at least after onCreate has been called)
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById<Button>(R.id.foo)
}

Related

My app is crashing when I use swicth statement or if statement. I dont why, please help me out

I think is happening in the switch case or there is any small error in my code.
Please help me out
Here is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/edText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180sp"
/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="230sp"
android:text="1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="230sp"
android:text="2"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="230sp"
android:text="3"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="280sp"
android:text="4" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="280sp"
android:text="5"/>
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="280sp"
android:text="6"/>
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="330sp"
android:text="7" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="330sp"
android:text="8"/>
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="330sp"
android:text="9"/>
<Button
android:id="#+id/buttonplus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="380sp"
android:text="+" />
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="380sp"
android:text="0"/>
<Button
android:id="#+id/buttoneq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="380sp"
android:text="="/>
<Button
android:id="#+id/buttondiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="430sp"
android:text="/" />
<Button
android:id="#+id/buttonmin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="430sp"
android:text="-"/>
<Button
android:id="#+id/buttonmul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="430sp"
android:text="*"/>
</RelativeLayout>
MainActivity.java:
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
int operator = 0;
int s1 = 0,s2 = 0,add = 0,min = 0,mul = 0,div = 0;
EditText tv;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,beq,bplus,bmin,bmul,bdiv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
b5 = (Button)findViewById(R.id.button5);
b6 = (Button)findViewById(R.id.button6);
b7 = (Button)findViewById(R.id.button7);
b8 = (Button)findViewById(R.id.button8);
b9 = (Button)findViewById(R.id.button9);
b0 = (Button)findViewById(R.id.button0);
beq = (Button)findViewById(R.id.buttoneq);
bplus = (Button)findViewById(R.id.buttonplus);
bmin = (Button)findViewById(R.id.buttonmin);
bmul = (Button)findViewById(R.id.buttonmul);
bdiv = (Button)findViewById(R.id.buttondiv);
tv = (EditText) findViewById(R.id.edText);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "9");
}
});
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText( tv.getText() + "0");
}
});
bplus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 1;
}
});
bmin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 2;
}
});
bmul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 3;
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 4;
}
});
beq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s2 = Integer.parseInt(tv.getText().toString());
add = s1 + s2;
min = s1 - s2;
mul = s1 * s2;
div = s1 / s2;
switch (operator){
case 1:
tv.setText(add);
break;
case 2:
tv.setText(min);
break;
case 3:
tv.setText(mul);
break;
case 4:
tv.setText(div);
break;
}
}
});
}
}
I have added 15 buttons to my calculator and everything works properly except when I click on equal to the button. Every button does works properly and prints the proper number into the textView when clicked but still when I click on the equal to button, my app just shuts down itself whenever I click on the equal to button on my calculator and nothing happens.
It seems like you need to parse integers to strings before you set the text. Try using String.valueOf() to parse add, min, mul, and div to strings.
Also, if you post your logcat, it'll be much easier to figure out what the problem is as it tells you what and where the error is.
You can not set integer value on Textview, only String will be acceptable. Try with the below code.
switch (operator){
case 1:
tv.setText(""+add);
break;
case 2:
tv.setText(""+min);
break;
case 3:
tv.setText(""+mul);
break;
case 4:
tv.setText(""+div);
break;
}
if you give an int value to setText(), it will be seen as a resId,so ,if system can not find this id, will throw new NotFoundException("String resource ID #0x"
+ Integer.toHexString(id));
Change eq listener to
beq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(tv.getText().toString().length()==0)
return;
s2 = Integer.parseInt(tv.getText().toString());
add = s1 + s2;
min = s1 - s2;
mul = s1 * s2;
div = s1 / s2;
switch (operator) {
case 1:
tv.setText(String.valueOf(add));
break;
case 2:
tv.setText(String.valueOf(min));
break;
case 3:
tv.setText(String.valueOf(mul));
break;
case 4:
tv.setText(String.valueOf(div));
break;
}
}
});
Do not set integer value to text view directly. First cast it to string.
Try replacing this in your code.

Edittext is not retrieving data in Android

My problem is that when i try to take data from the edittext fields(uidreg, passwordreg) it fails.
and this if(!uid.equals("") || !password.equals("")) becomes false and prints the toast Toast.makeText(getApplicationContext(),"All fields are required",Toast.LENGTH_LONG).show();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reg);
Button studentbtn = (Button)findViewById(R.id.studentbtn);
EditText textuidreg = (EditText)findViewById(R.id.uidreg);
EditText textpasswordreg = (EditText)findViewById(R.id.passwordreg);
String uid = textuidreg.getText().toString();
String password = textpasswordreg.getText().toString();
Toast.makeText(getApplicationContext(),uid,Toast.LENGTH_LONG).show();
studentbtn.setMovementMethod(LinkMovementMethod.getInstance());
studentbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!uid.equals("") || !password.equals("")) {
}
else {
Toast.makeText(getApplicationContext(),"All fields are required",Toast.LENGTH_LONG).show();
}
}
});
teacherbtn.setMovementMethod(LinkMovementMethod.getInstance());
teacherbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:orientation="vertical">
<EditText
android:id="#+id/uidreg"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/ic_uid"
android:ems="10"
android:hint="uid"
android:importantForAutofill="no"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.327" />
<EditText
android:id="#+id/passwordreg"
android:layout_width="214dp"
android:layout_height="40dp"
android:drawableRight="#drawable/ic_password"
android:ems="20"
android:hint="password"
android:importantForAutofill="no"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.528" />
</androidx.constraintlayout.widget.ConstraintLayout>
You need to put
String uid = textuidreg.getText().toString();
String password = textpasswordreg.getText().toString();
inside onClick function
full code should be like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reg);
Button studentbtn = (Button)findViewById(R.id.studentbtn);
EditText textuidreg = (EditText)findViewById(R.id.uidreg);
EditText textpasswordreg = (EditText)findViewById(R.id.passwordreg);
Toast.makeText(getApplicationContext(),uid,Toast.LENGTH_LONG).show();
studentbtn.setMovementMethod(LinkMovementMethod.getInstance());
studentbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String uid = textuidreg.getText().toString();
String password = textpasswordreg.getText().toString();
if(!uid.equals("") || !password.equals("")) {
}
else {
Toast.makeText(getApplicationContext(),"All fields are required",Toast.LENGTH_LONG).show();
}
}
});
teacherbtn.setMovementMethod(LinkMovementMethod.getInstance());
teacherbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}

"NaN" error when using operator in my calculator app

So I have been working on a Calculator app off a tutorial I got online. Everything's fine so far but I can't seem work to out what's causing this "NaN" error in my application every time I try to use the operator buttons. I'll attach screenshots of the code and app. Sorry if I'm vague I'm new to coding and just trying to learn.
My MainActivity.java
package com.example.calculator_prjct;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.app.Activity;
import com.example.calculator_prjct.databinding.ActivityMainBinding;
import android.os.Bundle;
import android.view.View;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
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 valueOne = Double.NaN;
private double valueTwo;
private DecimalFormat decimalFormat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
decimalFormat = new DecimalFormat("#.##########");
final ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.buttonDot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + ".");
}
});
binding.buttonZero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "0");
}
});
binding.buttonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "1");
}
});
binding.buttonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "2");
}
});
binding.buttonThree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "3");
}
});
binding.buttonFour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "4");
}
});
binding.buttonFive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "5");
}
});
binding.buttonSix.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "6");
}
});
binding.buttonSeven.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "7");
}
});
binding.buttonEight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "8");
}
});
binding.buttonNine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "9");
}
});
binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = ADDITION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});
binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
binding.editText.setText(null);
}
});
binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
binding.editText.setText(null);
}
});
binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = DIVISION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
binding.editText.setText(null);
}
});
binding.buttonEquals.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne));
valueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});
binding.buttonClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (binding.editText.getText().length() > 0) {
CharSequence currentText = binding.editText.getText();
binding.editText.setText(currentText.subSequence(0, currentText.length() - 1));
} else {
valueOne = Double.NaN;
valueTwo = Double.NaN;
binding.editText.setText("");
binding.infoTextView.setText("");
}
}
});
}
private void computeCalculation() {
if (!Double.isNaN(valueOne)) {
valueTwo = Double.parseDouble(binding.editText.getText().toString());
binding.editText.setText(null);
if (CURRENT_ACTION == ADDITION)
valueOne = this.valueOne + valueTwo;
else if (CURRENT_ACTION == SUBTRACTION)
valueOne = this.valueOne - valueTwo;
else if (CURRENT_ACTION == MULTIPLICATION)
valueOne = this.valueOne * valueTwo;
else if (CURRENT_ACTION == DIVISION)
valueOne = this.valueOne / valueTwo;
} else {
try {
valueOne = Double.parseDouble(binding.editText.getText().toString());
} catch (Exception e) {
}
}
}
}
My activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<RelativeLayout
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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/infoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:textSize="30sp" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/infoTextView"
android:enabled="false"
android:gravity="bottom"
android:lines="2"
android:maxLines="2"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSeven"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:text="7"
android:textSize="20sp" />
<Button
android:id="#+id/buttonFour"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="1dp"
android:layout_marginLeft="-87dp"
android:layout_marginTop="47dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="4"
android:textSize="20sp" />
<Button
android:id="#+id/buttonFive"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_marginLeft="1dp"
android:layout_marginTop="47dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="5"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSix"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_marginLeft="89dp"
android:layout_marginTop="46dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="6"
android:textSize="20sp" />
<Button
android:id="#+id/buttonOne"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="-1dp"
android:layout_marginLeft="-89dp"
android:layout_marginTop="96dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="1"
android:textSize="20sp" />
<Button
android:id="#+id/buttonTwo"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_marginLeft="-1dp"
android:layout_marginTop="95dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="2"
android:textSize="20sp" />
<Button
android:id="#+id/buttonThree"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_marginLeft="88dp"
android:layout_marginTop="97dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="3"
android:textSize="20sp" />
<Button
android:id="#+id/buttonDot"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="2dp"
android:layout_marginLeft="-86dp"
android:layout_marginTop="144dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="."
android:textSize="20sp" />
<Button
android:id="#+id/buttonZero"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="90dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="144dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="0"
android:textSize="20sp" />
<Button
android:id="#+id/buttonEquals"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="179dp"
android:layout_marginLeft="91dp"
android:layout_marginTop="145dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="="
android:textSize="20sp" />
<Button
android:id="#+id/buttonClear"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="265dp"
android:layout_marginLeft="177dp"
android:layout_marginTop="197dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="C"
android:textSize="20sp" />
<Button
android:id="#+id/buttonEight"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_toRightOf="#id/buttonSeven"
android:text="8"
android:textSize="20sp" />
<Button
android:id="#+id/buttonNine"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_toRightOf="#id/buttonEight"
android:text="9"
android:textSize="20sp" />
<Button
android:id="#+id/buttonDivide"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonNine"
android:layout_toRightOf="#id/buttonNine"
android:text="/"
android:textSize="20sp" />
<Button
android:id="#+id/buttonMultiply"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonDivide"
android:layout_marginLeft="-88dp"
android:layout_marginTop="48dp"
android:layout_toRightOf="#id/buttonDivide"
android:text="*"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSubtract"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonMultiply"
android:layout_marginLeft="-86dp"
android:layout_marginTop="53dp"
android:layout_toRightOf="#id/buttonMultiply"
android:text="-"
android:textSize="20sp" />
<Button
android:id="#+id/buttonAdd"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonSubtract"
android:layout_marginLeft="-89dp"
android:layout_marginTop="49dp"
android:layout_toRightOf="#id/buttonSubtract"
android:text="+"
android:textSize="20sp" />
</RelativeLayout>
</layout>
Screenshot of the actual error
"NaN Error on all operator buttons"
This is because you didn't check whether or not the EditText is empty in the respective onClick method of all the operator buttons. Add code like this to all of the operator buttons :
binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = ADDITION;
//if editText was empty then valueOne would still be NaN
//and infoTextView will show NaN+ with this line of code
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});
Full Activity Code :
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
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 valueOne = Double.NaN;
private double valueTwo;
private DecimalFormat decimalFormat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
decimalFormat = new DecimalFormat("#.##########");
binding.buttonDot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + ".");
}
});
binding.buttonZero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "0");
}
});
binding.buttonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "1");
}
});
binding.buttonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "2");
}
});
binding.buttonThree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "3");
}
});
binding.buttonFour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "4");
}
});
binding.buttonFive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "5");
}
});
binding.buttonSix.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "6");
}
});
binding.buttonSeven.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "7");
}
});
binding.buttonEight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "8");
}
});
binding.buttonNine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "9");
}
});
binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = ADDITION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});
binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
binding.editText.setText(null);
}
});
binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
binding.editText.setText(null);
}
});
binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = DIVISION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
binding.editText.setText(null);
}
});
binding.buttonEquals.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne));
valueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});
binding.buttonClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (binding.editText.getText().length() > 0) {
CharSequence currentText = binding.editText.getText();
binding.editText.setText(currentText.subSequence(0, currentText.length() - 1));
} else {
valueOne = Double.NaN;
valueTwo = Double.NaN;
binding.editText.setText("");
binding.infoTextView.setText("");
}
}
});
}
private void computeCalculation() {
if (!Double.isNaN(valueOne)) {
valueTwo = Double.parseDouble(binding.editText.getText().toString());
binding.editText.setText(null);
if (CURRENT_ACTION == ADDITION)
valueOne = this.valueOne + valueTwo;
else if (CURRENT_ACTION == SUBTRACTION)
valueOne = this.valueOne - valueTwo;
else if (CURRENT_ACTION == MULTIPLICATION)
valueOne = this.valueOne * valueTwo;
else if (CURRENT_ACTION == DIVISION)
valueOne = this.valueOne / valueTwo;
} else {
try {
valueOne = Double.parseDouble(binding.editText.getText().toString());
} catch (Exception e) {
}
}
}
}

code for image run in arraylist

i do array list with images thet i want them to run with 2 buttons thet go next pic and beck pic i dont know the code for this i thx all for the time you spend for help.
ackage com.example.hanansanag.mytourneyccreator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
/**
* Created by ssh on 25/12/2016.
*/
public class Players extends AppCompatActivity implements View.OnClickListener {
protected Button btnNext, btnBack;
protected String fname;
protected String Lname;
protected String team;
protected ImageView iv;
protected ArrayList array_image;
int i = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_pic);
ArrayList<Integer> array_image = new ArrayList<Integer>();
array_image.add(R.drawable.bacelona);
array_image.add(R.drawable.athlethiko);
array_image.add(R.drawable.arsenak);
array_image.add(R.drawable.chelsea);
array_image.add(R.drawable.dortmond);
array_image.add(R.drawable.city);
array_image.add(R.drawable.bayernunchen);
array_image.add(R.drawable.intermilan);
array_image.add(R.drawable.psj);
array_image.add(R.drawable.realmadrid);
array_image.add(R.drawable.leverpool);
array_image.add(R.drawable.milan);
array_image.add(R.drawable.juventus);
array_image.add(R.drawable.ashkelon);
array_image.add(R.drawable.macabiheifa);
array_image.add(R.drawable.macabitelaviv);
array_image.add(R.drawable.beitaryeroshlaim);
array_image.add(R.drawable.apoelbersheva);
iv = (ImageView) findViewById(R.id.imageView);
btnNext = (Button) findViewById(R.id.btnNextPic);
btnBack = (Button) findViewById(R.id.btnBeckPic);
btnBack.setOnClickListener(this);
btnNext.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (i <= 0 || i >= array_image.length) {
return;
}
if (btnNext == v) {
iv.setImageResource(array_image.get(i++));
} else if (btnBack == v) {
iv.setBackgroundResource(array_image.get(i--));
}
}
}
**what can i do here in this line of code thet the pic will move : **
#Override
public void onClick(View v) {
if (i <= 0 || i >= array_image.length) {
return;
}
if (btnNext == v) {
iv.setImageResource(array_image.get(i++));
} else if (btnBack == v) {
iv.setBackgroundResource(array_image.get(i--));
}
}
this is the xml;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/teamimage">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="Fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="12dp"
android:id="#+id/textView2" />
<ImageView
android:layout_height="80dp"
android:id="#+id/imageView"
android:layout_width="80dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:id="#+id/Lname"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/imageView" />
<TextView
android:text="Fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="12dp"
android:id="#+id/textView3" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true">
<Button
android:layout_width="20dp"
android:layout_height="15dp"
android:id="#+id/btnBeckPic"
android:background="#drawable/btnbeck"
android:layout_marginStart="30dp"
android:layout_alignBottom="#+id/Lname"
android:layout_toEndOf="#+id/textView2" />
<Button
android:background="#drawable/btnnext"
android:layout_width="20dp"
android:layout_height="15dp"
android:id="#+id/btnNextPic"
android:layout_below="#+id/imageView"
android:layout_toEndOf="#+id/btnBeckPic"
android:layout_marginStart="14dp" />
</GridLayout>
</RelativeLayout>
int currentImage = 0;
String[] strArr = new String[array_image.size()];
strArr = array_image.toArray(stockArr);
#Override
public void onClick(View v) {
if (btnNext == v) {
currentImage++;
currentImage = currentImage % strArr .length;
iv.setImageResource(strArr .length[currentImage]);
} else if (btnBack == v) {
currentImage--;
currentImage = (currentImage + strArr .length) % strArr .length;
iv.setImageResource(strArr[currentImage]);
}
}

Why isn't this code working?

I'm making a very simple calculator on Android. It shows no error on eclipse, but when I run it on my Android device, it stops working on clicking the buttons.
Here's my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.simplecalculator.MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="#string/enter_a_no"
android:inputType="numberSigned|numberDecimal"
/>
<requestFocus />
<Button
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/plus"
android:layout_alignBottom="#+id/plus"
android:layout_toRightOf="#+id/plus"
android:text="#string/minus"
android:onClick="minusClick" />
<Button
android:id="#+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/plus"
android:layout_below="#+id/plus"
android:text="#string/multiply"
android:onClick="multiplyClick"/>
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/multiply"
android:layout_alignBottom="#+id/multiply"
android:layout_alignLeft="#+id/minus"
android:text="#string/divide"
android:onClick="divideClick" />
<Button
android:id="#+id/equals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/divide"
android:layout_alignTop="#+id/minus"
android:layout_toRightOf="#+id/minus"
android:onClick="equalClick"
android:text="#string/equals" />
<Button
android:id="#+id/_1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/plus"
android:layout_below="#+id/editText1"
android:layout_marginTop="14dp"
android:text="#string/_1"
android:onClick="_1click"/>
<Button
android:id="#+id/_2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/_1"
android:layout_alignBottom="#+id/_1"
android:layout_toRightOf="#+id/_1"
android:text="#string/_2"
android:onClick="_2click"/>
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/divide"
android:layout_alignBottom="#+id/divide"
android:layout_toRightOf="#+id/divide"
android:onClick="clearClick"
android:text="#string/ce" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/_1"
android:layout_marginLeft="33dp"
android:layout_marginTop="127dp"
android:hint="#string/plus"
android:onClick="plusClick"
android:text="#string/plus" />
<Button
android:id="#+id/_4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_1"
android:layout_toLeftOf="#+id/_2"
android:text="#string/_4"
android:onClick="_4click"/>
<Button
android:id="#+id/_5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/_4"
android:layout_alignBottom="#+id/_4"
android:layout_alignLeft="#+id/_2"
android:text="#string/_5"
android:onClick="_5click" />
<Button
android:id="#+id/_6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/_5"
android:layout_alignBottom="#+id/_5"
android:layout_toRightOf="#+id/_5"
android:text="#string/_6"
android:onClick="_6click"/>
<Button
android:id="#+id/_7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_4"
android:layout_toLeftOf="#+id/_5"
android:text="#string/_7"
android:onClick="_7click"/>
<Button
android:id="#+id/_8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_5"
android:layout_toLeftOf="#+id/_6"
android:text="#string/_8"
android:onClick="_8click"/>
<Button
android:id="#+id/_9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_5"
android:layout_toRightOf="#+id/_5"
android:text="#string/_9"
android:onClick="_9click"/>
<Button
android:id="#+id/_3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/_5"
android:layout_toRightOf="#+id/_2"
android:text="#string/_3"
android:onClick="_3click"/>
<Button
android:id="#+id/_0"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_6"
android:layout_toRightOf="#+id/_6"
android:text="#string/_0"
android:onClick="_0click"/>
</RelativeLayout>
Here's my Java code:
package com.example.simplecalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private double vrble;
private double rslt;
private double vrble2;
String text;
EditText etext;
Button plus;
Button minus;
Button multiply;
Button divide;
Button equal;
#
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
vrble = 0;
rslt = 0;
etext = (EditText) findViewById(R.id.editText1);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);
equal = (Button) findViewById(R.id.equals);
} else {
vrble = 0;
rslt = 0;
vrble2 = 0;
etext = (EditText) findViewById(R.id.editText1);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);
equal = (Button) findViewById(R.id.equals);
}
}
public void _0click(View v) {
text = etext.getText().toString();
etext.setText(text + "0");
}
public void _1click(View v) {
text = etext.getText().toString();
etext.setText(text + "1");
}
public void _2click(View v) {
text = etext.getText().toString();
etext.setText(text + "2");
}
public void _3click(View v) {
text = etext.getText().toString();
etext.setText(text + "3");
}
public void _4click(View v) {
text = etext.getText().toString();
etext.setText(text + "4");
}
public void _5click(View v) {
text = etext.getText().toString();
etext.setText(text + "5");
}
public void _6click(View v) {
text = etext.getText().toString();
etext.setText(text + "6");
}
public void _7click(View v) {
text = etext.getText().toString();
etext.setText(text + "7");
}
public void _8click(View v) {
text = etext.getText().toString();
etext.setText(text + "8");
}
public void _9click(View v) {
text = etext.getText().toString();
etext.setText(text + "9");
}
public void plusClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble + vrble2;
}
public void minusClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble - vrble2;
}
public void multiplyClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble * vrble2;
}
public void divideClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble / vrble2;
}
public void clearClick(View v) {
vrble = 0;
rslt = 0;
etext.setText("0");
}
public void equalClick(View v) {
etext.setText("" + rslt);
}#
Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#
Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
What should I do?!!?!
One issue that I can se is a NumberFormatException that you might get. See these lines in any of your method like plusClick():
vrble = Double.parseDouble(etext.getText().toString());
etext.setText(""); //setting EditText to empty
vrble2 = Double.parseDouble(etext.getText().toString()); // parsing the empty string to Double
You are setting the eText to "" and then you are parsing it to Double in the next line. This is throwing the error. You might need to clear your login for all the actions like plus minus etc.
For more, your stacktrace will be helpful.

Categories