2016-08-18 49 views
0

所有的數字按鈕都工作正常,但是,只要按下相同的按鍵,應用程序崩潰,計算器就不會執行任何計算。如果我只是按下+, - ,*,/鍵而不輸入任何數字,應用程序也會崩潰。這是我的主要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個數字,並按下等於,然後我的應用程序崩潰。

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 /過程:發送信號。 PID:4671 SIG:9

+0

錯誤日誌漂亮請在糖頂部?我首先意識到的是,你的'if'塊在操作onClickListener'說如果計算爲空,將計算文本設置爲'「」',這是不可能達到的,因爲你已經聲明並初始化了'EditText'計算'以前,所以我認爲你應該這樣做,如果'calculate.getText()。equals(「」)'或什麼 –

回答

0

初始化您的Boolean值。

Boolean add = false, sub = false, div = false, multi = false; 

更新您的onClickplusminus方法,multiply & divide

@Override 
public void onClick(View view) { 
    if (calculation.getText().toString().trim().length() > 0) { 
     ValueOne = Float.parseFloat(calculation.getText() + ""); 
     add = true; // Change variable correspondingly for sub, multi & div 
     calculation.setText(null); 
    } 
} 
+0

我也用 不要使用calculation.setText(null); 使用calculate.setText(「」);而不是 現在我可以做計算,但仍然當我按任何運算符鍵而沒有輸入任何數字的應用程序崩潰。 –

+0

@AkshaySinghal多數民衆贊成那是因爲你想在拋出NumberFormatException的空文本Float.parseFloat。查看更新的答案。 – sJy

0

不要使用calculation.setText(null);

使用calculation.setText("");代替

0

ü前初始化布爾在條件下唱歌是非常必要的。否則它會彈出一個空對象引用。

所以你需要在函數中執行之前初始化一個布爾值給每個布爾項。

你的情況:

boolean add=false, sub=false , multi = false, div= false; 

初始化像這樣,你的錯誤將得到解決。