数字のボタンはすべて正常に機能していますが、等しいキーが押されたときに計算が実行されません。私がちょうど+、 - 、*、/キーを押しても、何も入力せずにアプリケーションもクラッシュします。ここに私のメインJavaクラスのコードです。Androidの電卓は等価ボタンが押されたときに計算を実行せずクラッシュします
public class MainActivity extends AppCompatActivity {
float ValueOne, ValueTwo;
Boolean add, sub, div, multi ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.one);
Button two = (Button) findViewById(R.id.two);
Button three = (Button) findViewById(R.id.three);
Button four = (Button) findViewById(R.id.four);
Button five = (Button) findViewById(R.id.five);
Button six = (Button) findViewById(R.id.six);
Button seven = (Button) findViewById(R.id.seven);
Button eight = (Button) findViewById(R.id.eight);
Button nine = (Button) findViewById(R.id.nine);
Button zero = (Button) findViewById(R.id.zero);
Button decimal = (Button) findViewById(R.id.decimal);
Button plus = (Button) findViewById(R.id.plus);
Button minus = (Button) findViewById(R.id.minus);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
Button del = (Button) findViewById(R.id.del);
Button eq = (Button) findViewById(R.id.eq);
final EditText calculation = (EditText) findViewById(R.id.calculation);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "1");
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "2");
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "3");
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "4");
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "5");
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "6");
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "9");
}
});
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "0");
}
});
decimal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + ".");
}
});
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (calculation == null) {
calculation.setText("");
}
else {
ValueOne = Float.parseFloat(calculation.getText() + "");
add = true;
calculation.setText(null);
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (calculation == null) {
calculation.setText("");
} else {
ValueOne = Float.parseFloat(calculation.getText() + "");
sub = true;
calculation.setText(null);
}
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (calculation == null) {
calculation.setText("");
} else {
ValueOne = Float.parseFloat(calculation.getText() + "");
multi = true;
calculation.setText(null);
}
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (calculation == null) {
calculation.setText("");
} else {
ValueOne = Float.parseFloat(calculation.getText() + "");
div = true;
calculation.setText(null);
}
}
});
eq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ValueTwo = Float.parseFloat(calculation.getText() + "");
if (add == true) {
calculation.setText(ValueOne + ValueTwo + "");
add=false;
}
if(sub == true){
calculation.setText(ValueOne - ValueTwo + "");
sub=false;
}
if (multi == true){
calculation.setText(ValueOne*ValueTwo + "");
multi = false;
}
if (div == true){
calculation.setText(ValueOne/ValueTwo + "");
div = false;
}
}
});
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText("");
}
});
}
}
ここでは、2つの数字を追加しようとしたときにequalsを押したときに、アプリがクラッシュしました。
08-12 16:04:52.402 4671-4671/jarvis.calc E/AndroidRuntime: FATAL EXCEPTION: main
Process: jarvis.calc, PID: 4671
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at jarvis.calc.MainActivity$16.onClick(MainActivity.java:187)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-12 16:04:57.023 4671-4671/jarvis.calc I/Process:送信信号。 PID:4671 SIG:9
エラーログはかなり上に砂糖を添えてください?最初のことは、 'onClickListener'オペレーションの' if'ブロックが、計算がnullの場合、計算テキストを '' "'に設定していると言っています。これは、 'EditText'の計算を宣言して初期化したため'の前にあるので、もし' calculation.getText()。equals( "") 'または何か –