为什么在使用.settext()时会出现错误,但在方法中使用时却没有呢?

时间:2019-01-19 01:58:55

标签: java android

比较我的Increment和Decrement方法,为什么我得到一个关于Decrement的错误而不是一个Increment方法的错误?我对此有点陌生,因此如果这是一个愚蠢的问题,我表示歉意。

public class MainActivity extends AppCompatActivity {

    Button minusOne, button3, button4;
    TextView textView2, textView4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        minusOne = (Button) findViewById(R.id.minusOne);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);

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


    }

    int quanity = 0;

//    public int submitOrder(View textView) {
//        int price = Integer.parseInt(textView2.getText().toString());
//        return price;
//
//
//    }

    private void display(int number){
        textView2.setText("" + number);
    }


    public void increment(View view){
        quanity += 1; // this is just a global var
        TextView tempVar = (TextView) findViewById(R.id.textView2); // this new var = to the updated version
        display(quanity);    }

    public void decrement(View view){
        quanity -= 1;
        TextView tempVar = (TextView) findViewById(R.id.textView4);
        tempVar.setText(quanity);}

}

0 个答案:

没有答案