未知的android错误

时间:2016-10-05 18:37:36

标签: java android

有人可以帮助我使用代码,当我按下计算(按钮)时,模拟会突然停止。代码是基本上使用3个不同的edittext(etjan,etfeb,etmar)添加3个数字,然后计算佣金(在if ... else,etcom中),然后找到总数(etpro)。

package com.example.personal.semifinals_exam;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
// import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

  EditText etJAN, etFEB, etMAR, etTS, etCOM, etPRO;
  //TextView txtO_TS, txtO_COM, txtO_PRO;
  Button btnCALC/*, btnADD, btnDELETE, btnCLEAR*/;

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

    etJAN =(EditText) findViewById(R.id.etJAN);
    etFEB =(EditText) findViewById(R.id.etFEB);
    etMAR =(EditText) findViewById(R.id.etMAR);

    btnCALC = (Button) findViewById(R.id.btnCALC);
    btnCALC.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        String Jan, Feb, Mar;
        double com, tc;

        Jan = etJAN.getText().toString();
        Feb = etFEB.getText().toString();
        Mar = etMAR.getText().toString();

        double jan = Double.parseDouble(Jan);
        double feb = Double.parseDouble(Feb);
        double mar = Double.parseDouble(Mar);

        double ts = jan + feb + mar;

        etTS.setText(ts + " ");

        if(ts <= 299) {
          com = 0.00;
          tc = ts * com;
          etCOM.setText("No Commission");
          etPRO.setText((tc + ts) +" " );
        }
        else if (ts > 299 && ts < 1000) {
          com = 0.10;
          tc = ts * com;
          etCOM.setText("10% Commission");
          etPRO.setText((tc + ts) +" " );
        }
        else if (ts > 999 && ts < 1500) {  
          com = 0.20;
          tc = ts * com;
          etCOM.setText("20% Commission");
          etPRO.setText((tc + ts) +" " );
        }
      }
    });
  }
}

2 个答案:

答案 0 :(得分:0)

你没有在你的代码中初始化etTS,etCOM,etPRO,首先像你为etJAN,etFEB,etMAR做的那样初始化它们然后尝试为它们设置值......

答案 1 :(得分:0)

请尝试初始化 etTS,etCOM,etPRO ,然后使用setText

etTS =(EditText) findViewById(R.id.etTS);
etCOM =(EditText) findViewById(R.id.etCOM);
etPRO =(EditText) findViewById(R.id.etPRO);