Android 简单的计算器应用程序崩溃和逻辑
Android Simple Calculator App Crashing and Logic
我正在为一个简单的计算器制作一个 android 应用程序。最重要的部分是它使用了一个 "rolling" textView,所以例如如果你做了 1+1,然后如果你按下另一个算术运算符,例如“+”,textView 就会变成 2+(输入新数字,例如 3),然后如果您单击乘法,它将显示 5* 等等
现在错误出现在第 34 行,当我在计算器上单击数字 1 时显示空指针错误。想知道是否有人可以帮助我解决这个/其他逻辑错误,如果你看到的话。
基本上,如果您单击以在计算器上使用算术运算符,并且如果计算器上的 textView 中已经有一个运算符,那么它会计算当前的 textView 并在其旁边吐出新的算术运算符。否则如果计算器上没有算术运算符,它只是将运算符放在第一个数字旁边。
我想做的是:
if the user clicks a number, then set rollingBar to that number
if the user clicks an operator
if it is the first operator then there must be only 1 string inside of
the rollingBar, so assign that string to leftNum, and assign the first
operator type to the text of the button eg. "1".
get text in rolling bar, assign it to text1, make text2 and assign
it to text1 + stringButton(which would be 1,2,3,4 etc.. name of btn.
set the first operator to false
if it's not the first operator, then you need to calculate by splitting
the function at the first arithmetic operator, assign the last number
from the split into rightNum
switch to the first operator type
do the math
assign this second operator to the next rollingBar
现在我遇到了一些问题...我将展示一张照片和 java 文件...
Picture:
http://imgur.com/35hy4Ze
Java 文件:
包 com.example.w0273754.calcfinal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.lang.String;
public class MainActivity extends AppCompatActivity {
//declarations
TextView rollingBar;//where all of the text goes when the user clicks a button
String leftNum;
String rightNum;
boolean firstOptr;
String firstOptrType;
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollingBar = (TextView) findViewById(R.id.rollingBar);
leftNum = "";
rightNum = "";
firstOptr = true;
number = "";
}
public void number(View v){
Button button = (Button) v;
number += button.getText().toString();
rollingBar.setText(number);
}
//i can click 1 and it outputs 1
// i can click + and it outputs +
// when I click next number it just outputs the number, nothing else
public void operater(View v) {
Button button = (Button) v;
String stringButton = button.getText().toString();
//if it's the first operator being done, then set the rollbar text to text1,
// then add the operator string to text1 and store it in text 2.
if (firstOptr) {
leftNum = rollingBar.getText().toString();
firstOptrType = stringButton;
number += stringButton;
rollingBar.setText(number);
firstOptr = false;
}
//if it's not the first operator, then you need to calculate leftnum and the right num
else {
String fullString = (String) rollingBar.getText().toString();
String[] bits = fullString.split("[-+*/]");
rightNum = bits[bits.length - 1];
switch (firstOptrType) {
case "+":
add(leftNum, rightNum);
break;
case "-":
break;
case "*":
break;
case "/":
break;
default:
rollingBar.setText("OPERATOR METHOD PROBLEM");
}
}
}
public void add(String leftNum, String rightNum){
int parsedLeftNum = Integer.parseInt(leftNum);
int parsedRightNum = Integer.parseInt(rightNum);
rollingBar.setText(parsedLeftNum + parsedRightNum);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML 文件:
<TableLayout android:id="@+id/TableLayout"
tools:context=".MainActivity"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<TextView android:id="@+id/rollingBar" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="1" android:layout_span="4" android:text="@string/rollingBar"/>
</TableRow>
<TableRow android:layout_height="match_parent"
android:layout_width="match_parent">
<Button android:id="@+id/buttonClear" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/buttonClear" />
<Button android:id="@+id/buttonPlusMinus" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/buttonPlusMinus" android:onClick="onClick1"/>
<Button android:id="@+id/buttonDelete" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/buttonDelete" />
<Button android:id="@+id/buttonDivide" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="@string/buttonDivide" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<Button android:id="@+id/button7" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button7" android:onClick="number" android:nestedScrollingEnabled="false"/>
<Button android:id="@+id/button8" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button8" android:onClick="number"/>
<Button android:id="@+id/button9" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button9" android:onClick="number"/>
<Button android:id="@+id/buttonMultiply" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="@string/buttonMultiply" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<Button android:id="@+id/button4" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button4" android:onClick="number"/>
<Button android:id="@+id/button5" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button5" android:onClick="number"/>
<Button android:id="@+id/button6" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button6" android:onClick="number"/>
<Button android:id="@+id/buttonSubtract" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="@string/buttonSubtract" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="wrap_content" android:layout_width="match_parent">
<Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/button1" android:onClick="number"/>
<Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/button2" android:onClick="number"/>
<Button android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/button3" android:onClick="number"/>
<Button android:id="@+id/buttonAdd" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="@string/buttonAdd" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<Button android:id="@+id/button0" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="2" android:text="@string/button0" android:onClick="number"/>
<Button android:id="@+id/buttonDecimal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/buttonDecimal"/>
<Button android:id="@+id/buttonEqual" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/buttonEqual" android:onClick="operater"/>
</TableRow>
<TableRow android:layout_height="match_parent" android:layout_width="match_parent">
<TextView android:id="@+id/textView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="0"/>
</TableRow>
<TableRow android:layout_height="match_parent" android:layout_width="match_parent">
<TextView android:id="@+id/errorMessage" android:layout_height="wrap_content" android:layout_width="wrap_content"/>
</TableRow>
</TableLayout>
非常感谢您的帮助!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView rollingBar = (TextView) findViewById(R.id.rollingBar); // <-- HERE
String leftNum = "";
String rightNum = "";
boolean firstOptr = true;
String firstOptrType;
}
你在onCreate
中声明了一个局部变量rollingBar
,它隐藏了你Activity的成员变量,所以成员变量rollingBar
总是null,删除TextView
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollingBar = (TextView) findViewById(R.id.rollingBar);
String leftNum = "";
String rightNum = "";
boolean firstOptr = true;
String firstOptrType;
}
我正在为一个简单的计算器制作一个 android 应用程序。最重要的部分是它使用了一个 "rolling" textView,所以例如如果你做了 1+1,然后如果你按下另一个算术运算符,例如“+”,textView 就会变成 2+(输入新数字,例如 3),然后如果您单击乘法,它将显示 5* 等等
现在错误出现在第 34 行,当我在计算器上单击数字 1 时显示空指针错误。想知道是否有人可以帮助我解决这个/其他逻辑错误,如果你看到的话。
基本上,如果您单击以在计算器上使用算术运算符,并且如果计算器上的 textView 中已经有一个运算符,那么它会计算当前的 textView 并在其旁边吐出新的算术运算符。否则如果计算器上没有算术运算符,它只是将运算符放在第一个数字旁边。
我想做的是:
if the user clicks a number, then set rollingBar to that number
if the user clicks an operator
if it is the first operator then there must be only 1 string inside of
the rollingBar, so assign that string to leftNum, and assign the first
operator type to the text of the button eg. "1".
get text in rolling bar, assign it to text1, make text2 and assign
it to text1 + stringButton(which would be 1,2,3,4 etc.. name of btn.
set the first operator to false
if it's not the first operator, then you need to calculate by splitting
the function at the first arithmetic operator, assign the last number
from the split into rightNum
switch to the first operator type
do the math
assign this second operator to the next rollingBar
现在我遇到了一些问题...我将展示一张照片和 java 文件...
Picture:
http://imgur.com/35hy4Ze
Java 文件:
包 com.example.w0273754.calcfinal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.lang.String;
public class MainActivity extends AppCompatActivity {
//declarations
TextView rollingBar;//where all of the text goes when the user clicks a button
String leftNum;
String rightNum;
boolean firstOptr;
String firstOptrType;
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollingBar = (TextView) findViewById(R.id.rollingBar);
leftNum = "";
rightNum = "";
firstOptr = true;
number = "";
}
public void number(View v){
Button button = (Button) v;
number += button.getText().toString();
rollingBar.setText(number);
}
//i can click 1 and it outputs 1
// i can click + and it outputs +
// when I click next number it just outputs the number, nothing else
public void operater(View v) {
Button button = (Button) v;
String stringButton = button.getText().toString();
//if it's the first operator being done, then set the rollbar text to text1,
// then add the operator string to text1 and store it in text 2.
if (firstOptr) {
leftNum = rollingBar.getText().toString();
firstOptrType = stringButton;
number += stringButton;
rollingBar.setText(number);
firstOptr = false;
}
//if it's not the first operator, then you need to calculate leftnum and the right num
else {
String fullString = (String) rollingBar.getText().toString();
String[] bits = fullString.split("[-+*/]");
rightNum = bits[bits.length - 1];
switch (firstOptrType) {
case "+":
add(leftNum, rightNum);
break;
case "-":
break;
case "*":
break;
case "/":
break;
default:
rollingBar.setText("OPERATOR METHOD PROBLEM");
}
}
}
public void add(String leftNum, String rightNum){
int parsedLeftNum = Integer.parseInt(leftNum);
int parsedRightNum = Integer.parseInt(rightNum);
rollingBar.setText(parsedLeftNum + parsedRightNum);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML 文件:
<TableLayout android:id="@+id/TableLayout"
tools:context=".MainActivity"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<TextView android:id="@+id/rollingBar" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="1" android:layout_span="4" android:text="@string/rollingBar"/>
</TableRow>
<TableRow android:layout_height="match_parent"
android:layout_width="match_parent">
<Button android:id="@+id/buttonClear" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/buttonClear" />
<Button android:id="@+id/buttonPlusMinus" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/buttonPlusMinus" android:onClick="onClick1"/>
<Button android:id="@+id/buttonDelete" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/buttonDelete" />
<Button android:id="@+id/buttonDivide" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="@string/buttonDivide" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<Button android:id="@+id/button7" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button7" android:onClick="number" android:nestedScrollingEnabled="false"/>
<Button android:id="@+id/button8" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button8" android:onClick="number"/>
<Button android:id="@+id/button9" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button9" android:onClick="number"/>
<Button android:id="@+id/buttonMultiply" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="@string/buttonMultiply" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<Button android:id="@+id/button4" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button4" android:onClick="number"/>
<Button android:id="@+id/button5" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button5" android:onClick="number"/>
<Button android:id="@+id/button6" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button6" android:onClick="number"/>
<Button android:id="@+id/buttonSubtract" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="@string/buttonSubtract" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="wrap_content" android:layout_width="match_parent">
<Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/button1" android:onClick="number"/>
<Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/button2" android:onClick="number"/>
<Button android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/button3" android:onClick="number"/>
<Button android:id="@+id/buttonAdd" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="@string/buttonAdd" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<Button android:id="@+id/button0" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="2" android:text="@string/button0" android:onClick="number"/>
<Button android:id="@+id/buttonDecimal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/buttonDecimal"/>
<Button android:id="@+id/buttonEqual" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_span="1" android:text="@string/buttonEqual" android:onClick="operater"/>
</TableRow>
<TableRow android:layout_height="match_parent" android:layout_width="match_parent">
<TextView android:id="@+id/textView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="0"/>
</TableRow>
<TableRow android:layout_height="match_parent" android:layout_width="match_parent">
<TextView android:id="@+id/errorMessage" android:layout_height="wrap_content" android:layout_width="wrap_content"/>
</TableRow>
</TableLayout>
非常感谢您的帮助!
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView rollingBar = (TextView) findViewById(R.id.rollingBar); // <-- HERE String leftNum = ""; String rightNum = ""; boolean firstOptr = true; String firstOptrType; }
你在onCreate
中声明了一个局部变量rollingBar
,它隐藏了你Activity的成员变量,所以成员变量rollingBar
总是null,删除TextView
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollingBar = (TextView) findViewById(R.id.rollingBar);
String leftNum = "";
String rightNum = "";
boolean firstOptr = true;
String firstOptrType;
}