2016-08-28 125 views
0

我試圖製作一個簡單的計算器,但由於某些原因,我無法獲得結果。
該應用程序運行,但當我按下按鈕沒有彈出結果顯示,如預期。Android Studio中的應用程序問題

下面是代碼:

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.view.View.OnClickListener; 
import android.widget.Toast; 
public class MainActivity extends AppCompatActivity implements View.OnClickListener { 
    private EditText number1EditText; 
    private EditText number2EditText; 
    private Button divideButton; 
    private Button multiplyButton; 
    private Button addButton; 
    private Button subtractButton; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     number1EditText = (EditText) findViewById(R.id.number1EditText); 
     number2EditText = (EditText) findViewById(R.id.number2EditText); 
     divideButton=(Button)findViewById(R.id.divideButton); 
     multiplyButton=(Button)findViewById(R.id.multiplyButton); 
     addButton=(Button)findViewById(R.id.addButton); 
     subtractButton=(Button)findViewById(R.id.subtractButton); } 
    public void onClick(View v){ 
     switch (v.getId()){ 
      case R.id.divideButton: 
       division(); 
       break; 
      case R.id.multiplyButton: 
       multiplication(); 
       break; 
      case R.id.addButton: 
       addition(); 
       break; 
      case R.id.subtractButton: 
       subtraction(); 
       break;} } 
    private void division(){ 
     String number1 = number1EditText.getText().toString(); 
     String number2 = number2EditText.getText().toString(); 
     int answer = Integer.parseInt(number1)/Integer.parseInt(number2); 
     Toast.makeText(this,String.valueOf(answer), Toast.LENGTH_LONG).show();} 
    private void multiplication(){ 
     String number1=number1EditText.getText().toString(); 
     String number2=number2EditText.getText().toString(); 
     int answer=Integer.parseInt(number1)*Integer.parseInt(number2); 
     Toast.makeText(this,String.valueOf(answer),Toast.LENGTH_LONG).show(); } 
    private void addition(){ 
     String number1=number1EditText.getText().toString(); 
     String number2=number2EditText.getText().toString(); 
     int answer = Integer.parseInt(number1) + Integer.parseInt(number2); 
     Toast.makeText(this,String.valueOf(answer), Toast.LENGTH_LONG).show(); } 
    private void subtraction(){ 
     String number1=number1EditText.getText().toString(); 
     String number2=number2EditText.getText().toString(); 
     int answer=Integer.parseInt(number1) - Integer.parseInt(number2); 
     Toast.makeText(this,String.valueOf(answer),Toast.LENGTH_LONG).show(); }} 
+1

請添加相關的XML佈局代碼 – Zoe

回答

2

解決辦法很簡單,就在這裏:

divideButton=(Button)findViewById(R.id.divideButton); 
multiplyButton=(Button)findViewById(R.id.multiplyButton); 
addButton=(Button)findViewById(R.id.addButton); 
subtractButton=(Button)findViewById(R.id.subtractButton); 

找到的按鈕,但從來沒有設置onCLickListener。添加以下代碼:

divideButton.setOnClickListener(this); 
multiplyButton.setOnClickListener(this); 
addButton.setOnClickListener(this); 
subtractButton.setOnClickListener(this); 
+1

我猜OP使用'安卓的onClick()'在他/她的佈局的按鍵定義。這會讓onClick偵聽器中的'switch'塊變得有意義。 –

+0

也許,也許不是。沒有提供XML代碼,所以這看起來像問題 – Zoe

+0

我從'switch'塊的存在推導出我的推理 –