如果editText留为空白,我的应用程序将继续崩溃。如果我单击“添加”按钮时editText中没有任何内容,则我需要该应用程序假定该条目为“ 0”(零)。我对此是全新的,仅在最近几周内学习Java。我不确定自己的格式是否还不错,但是似乎可以正常工作,除了使应用程序崩溃的空白字段。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_front_half);
Button homeBtn = (Button) findViewById(R.id.homeBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startMainActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(startMainActivity);
}
});
Button addBtn = (Button) findViewById(R.id.FHAddBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final EditText FHEnter5RingEditText = (EditText) findViewById(R.id.FHEnter5RingEditText);
final EditText FHEnter4RingEditText = (EditText) findViewById(R.id.FHEnter4RingEditText);
final EditText FHEnter3RingEditText = (EditText) findViewById(R.id.FHEnter3RingEditText);
final EditText FHEnter2RingEditText = (EditText) findViewById(R.id.FHEnter2RingEditText);
final EditText FHEnterMissesEditText = (EditText) findViewById(R.id.FHEnterMissesEditText);
final TextView FHScoreTextView = (TextView) findViewById(R.id.FHScoreTextView);
final TextView FHRoundCountTextView = (TextView) findViewById(R.id.FHRoundCountTextView);
int FHring5 = Integer.parseInt(FHEnter5RingEditText.getText().toString());
int FHring4 = Integer.parseInt(FHEnter4RingEditText.getText().toString());
int FHring3 = Integer.parseInt(FHEnter3RingEditText.getText().toString());
int FHring2 = Integer.parseInt(FHEnter2RingEditText.getText().toString());
int FHring0 = Integer.parseInt(FHEnterMissesEditText.getText().toString());
int FHroundCount = FHring5 + FHring4 + FHring3 + FHring2 + FHring0;
int FHresult = (FHring5 * 5) + (FHring4 * 4) + (FHring3 * 3) + (FHring2 * 2);
FHRoundCountTextView.setText(FHroundCount + "");
FHScoreTextView.setText(FHresult + "");
if (FHresult < 120) {
FHScoreTextView.setTextColor(Color.RED);
} else if (FHresult > 150) {
FHScoreTextView.setTextColor(Color.RED);
}else{
FHScoreTextView.setTextColor(Color.GREEN);
}
if (FHroundCount > 30) {
FHRoundCountTextView.setTextColor(Color.RED);
}else{
FHRoundCountTextView.setTextColor(Color.BLACK);
}
Button FHClearBtn = (Button) findViewById(R.id.FHClearBtn);
FHClearBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FHEnter5RingEditText.setText("");
FHEnter4RingEditText.setText("");
FHEnter3RingEditText.setText("");
FHEnter2RingEditText.setText("");
FHEnterMissesEditText.setText("");
FHScoreTextView.setText("");
FHRoundCountTextView.setText("");
};
});
}
});
}
}