2016-02-07 54 views
0

我正在爲android做一個計算器。在添加代碼以將文本顯示到textView_1之前,所有的東西都可以正常工作,但在此之後,textView_1中沒有顯示任何文本,也沒有任何操作員正在工作。爲什麼文本不顯示在textView_1和操作員不工作

package com.example.anunay.basiccalculator1; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import java.lang.*; 

public class MainActivity extends AppCompatActivity { 

    Button button_0, button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, 
      button_9, button_Deci, button_Equal, button_Add, button_Sub, button_Mul, button_Div, 
      button_Back, button_Expo, button_Sqrt, button_Clear; 


    TextView textView_1, textView_2; 

    float mValue1, mValue2; 

// double mValue3; 

    String mString1, mString2, mString2_Rev, textView_1Value; 

    boolean mAddition = false, mSubtraction = false, mMultiplication = false, mDivision = false, 
      mEqualPressed = false, mSqrt = false, mExponential = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     button_0 = (Button) findViewById(R.id.button0); 
     button_1 = (Button) findViewById(R.id.button1); 
     button_2 = (Button) findViewById(R.id.button2); 
     button_3 = (Button) findViewById(R.id.button3); 
     button_4 = (Button) findViewById(R.id.button4); 
     button_5 = (Button) findViewById(R.id.button5); 
     button_6 = (Button) findViewById(R.id.button6); 
     button_7 = (Button) findViewById(R.id.button7); 
     button_8 = (Button) findViewById(R.id.button8); 
     button_9 = (Button) findViewById(R.id.button9); 
     button_Deci = (Button) findViewById(R.id.buttonDeci); 
     button_Add = (Button) findViewById((R.id.buttonAdd)); 
     button_Sub = (Button) findViewById(R.id.buttonSub); 
     button_Mul = (Button) findViewById(R.id.buttonMul); 
     button_Div = (Button) findViewById(R.id.buttonDiv); 
     button_Sqrt = (Button) findViewById(R.id.buttonSqrt); 
     button_Expo = (Button) findViewById(R.id.buttonExpo); 
     button_Back = (Button) findViewById(R.id.buttonBack); 
     button_Clear = (Button) findViewById(R.id.buttonClear); 
     button_Equal = (Button) findViewById(R.id.buttonEqual); 

     textView_2 = (TextView) findViewById(R.id.textView2); 



     button_0.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("0"); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"0"); 
       mEqualPressed = false; 
      } 
     }); 

     button_1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("1"); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"1"); 
       mEqualPressed = false; 
      } 
     }); 

     button_2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("2"); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"2"); 
       mEqualPressed = false; 
      } 
     }); 

     button_3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("3"); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"3"); 
       mEqualPressed = false; 
      } 
     }); 

     button_4.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("4"); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"4"); 
       mEqualPressed = false; 
      } 
     }); 

     button_5.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("5"); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"5"); 
       mEqualPressed = false; 
      } 
     }); 

     button_6.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("6"); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"6"); 
       mEqualPressed = false; 
      } 
     }); 

     button_7.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("7"); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"7"); 
       mEqualPressed = false; 
      } 
     }); 

     button_8.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("8"); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"8"); 
       mEqualPressed = false; 
      } 
     }); 

     button_9.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("9"); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"9"); 
       mEqualPressed = false; 
      } 
     }); 

     button_Deci.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mEqualPressed) { 
        textView_2.setText("."); 
        textView_1.setText(" "); 

       } 
       else textView_2.setText(textView_2.getText()+"."); 
       mEqualPressed = false; 
      } 
     }); 

     button_Add.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (textView_2.getText()==""){ 
        textView_2.setText(""); 
       } 
       else if (textView_2.getText()=="-") 
        textView_2.setText(" "); 

       else 
       { 
        textView_1.setText(textView_2.getText().toString()+"+"); 
       mValue1 = Float.parseFloat(textView_2.getText() + ""); 
       mAddition = true; 
       textView_2.setText(null); 
       mEqualPressed = false;} 
      } 
     }); 

     button_Sub.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (textView_2.getText()==""){ 
        textView_2.setText("-"); 
       } 
       else if (textView_2.getText()=="-") 
        textView_2.setText("-"); 
       else{ 
       textView_1.setText(textView_2.getText().toString()+"-"); 
       mValue1 = Float.parseFloat(textView_2.getText() + ""); 
       mSubtraction = true; 
       textView_2.setText(null); 
       mEqualPressed = false;} 
      } 
     }); 

     button_Mul.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (textView_2.getText()==""){ 
        textView_2.setText(""); 
       } 
       else if (textView_2.getText()=="-") 
        textView_2.setText(" "); 
       else{ 
        textView_1.setText(textView_2.getText().toString()+"*"); 
       mValue1 = Float.parseFloat(textView_2.getText() + ""); 
       mMultiplication = true; 
       textView_2.setText(null); 
       mEqualPressed = false;} 
      } 
     }); 

     button_Div.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (textView_2.getText()==""){ 
        textView_2.setText(""); 
       } 
       else if (textView_2.getText()=="-") 
        textView_2.setText(" "); 
       else{ 
       textView_1.setText(textView_2.getText().toString()+"/"); 
       mValue1 = Float.parseFloat(textView_2.getText() + ""); 
       mDivision = true; 
       textView_2.setText(null); 
       mEqualPressed = false;} 
      } 
     }); 

     button_Sqrt.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       mEqualPressed = false; 
       mSqrt = true; 
      } 
     }); 

     button_Expo.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (textView_2.getText()==""){ 
        textView_2.setText(""); 
       } 
       else{ 
        textView_1.setText(textView_2.getText()+"^"); 
       mValue1 = Float.parseFloat(textView_2.getText() + ""); 
       mExponential = true; 
       textView_2.setText(null); 
       mEqualPressed = false;} 

      } 
     }); 

     button_Back.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (textView_2.getText().length()<=1) 
        textView_2.setText(" "); 
       else 
        textView_2.setText((textView_2.getText()).subSequence(0, (textView_2.getText()).length()-1)); 


      } 
     }); 

     button_Clear.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       textView_2.setText(" "); 
       mEqualPressed = false; 

      } 
     }); 

     button_Equal.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mValue2 = Float.parseFloat(textView_2.getText() + ""); 

       textView_1.setText(textView_1.getText() + textView_2.getText().toString()); 

       if (mAddition){ 

        textView_2.setText(mValue1 + mValue2 +""); 
        mAddition=false; 
       } 


       if (mSubtraction){ 
        textView_2.setText(mValue1 - mValue2+""); 
        mSubtraction=false; 
       } 

       if (mMultiplication){ 
        textView_2.setText(mValue1 * mValue2+""); 
        mMultiplication=false; 
       } 

       if (mDivision){ 
        textView_2.setText(mValue1/mValue2+""); 
        mDivision=false; 
       } 

       if (mSqrt){ 

        textView_2.setText(Math.sqrt((double)mValue2)+""); 
        mSqrt = false; 

       } 

       if (mExponential){ 

        textView_2.setText(Math.pow((double) mValue1, (double)mValue2)+""); 
        mExponential = false; 
       } 

       mEqualPressed = true; 
      } 
     }); 


    } 
} 
+0

你試過調試嗎? – Mangesh

回答

1

您加入這個

textView_2 = (TextView) findViewById(R.id.textView2); 

卻忘了初始化爲textView_1參考。

+0

非常感謝。我只想徘徊錯誤是什麼。 – Anunay

+0

如果它幫助你解決問題,你可以接受答案:) –

相關問題