gpt4 book ai didi

android - 按下退格键时应用程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:23 24 4
gpt4 key购买 nike

我正在尝试在构建 Android 应用程序的同时学习 Java。我有一个没有按钮的积分计算器,它使用文本更改监听器来计算总数。当按下退格键并且该框为空时,它会崩溃。我尝试使用下面的代码进行验证(仅在字段开始时进行验证)。但它不起作用。任何帮助将不胜感激。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wwcalc);
etFat = (EditText)findViewById(R.id.editTextFat);
etFiber = (EditText)findViewById(R.id.editTextFiber);
etProtein = (EditText)findViewById(R.id.editTextProtein);
etCarbs = (EditText)findViewById(R.id.editTextCarbs);
tvTotal = (TextView)findViewById(R.id.textViewPoints);

TextWatcher watcher = new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(isEmpty(etFat) == false){
intFat = Integer.parseInt(etFat.getText().toString());
}
else{

etFat.setText("0");
etFat.hasFocus();
return;
}
intProtein = Integer.parseInt(etProtein.getText().toString());
intFiber = Integer.parseInt(etFiber.getText().toString());
intCarbs = Integer.parseInt(etCarbs.getText().toString());
calculate();
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub

}


};

etFat.addTextChangedListener(watcher);
etProtein.addTextChangedListener(watcher);
etFiber.addTextChangedListener(watcher);
etCarbs.addTextChangedListener(watcher);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_wwcalc, menu);

return true;

}
public void calculate(){
//intTot = intFat + intCarbs + intFiber + intProtein;
intTot = (int) Math.ceil((intFat * (4/35)) + (intCarbs * (4/36.84)) - (intFiber* (4/50))+ (intProtein * (4/43.75)) ) ;
tvTotal.setText(Integer.toString(intTot));
}
private boolean isEmpty(EditText etText)
{
if(etText.getText().toString().trim().length() > 0 || etText.getText().toString().trim() != null)
return false;
else
return true;
}

感谢大家的帮助。如果有人认为有更好的方法让我知道,我不确定它是否是最佳解决方案。 conor 建议的 try catch 捕获异常,然后只需插入 0 设置焦点并选择 0

try {
intFat = Integer.parseInt(etFat.getText().toString());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
etFat.setText("0");
etFat.hasFocus();
etFat.selectAll();
}

最佳答案

在没有看到堆栈跟踪的情况下很难知道,但无论如何我都会尝试一下。

isEmpty() 函数的以下部分允许在框可能仍为空时返回 false。

etText.getText().toString().trim() != null

这意味着当您按下退格键并清除该字段时,它是空的,但您的函数却说它不是。然后更重要的是,当您的应用程序认为该字段不为空时,它会尝试解析内容以获得整数值(不存在)。这种解析尝试会引发异常并导致您的应用崩溃。

我希望堆栈跟踪显示应用程序在此行崩溃

intFat = Integer.parseInt(etFat.getText().toString());

值得注意的是,您应该始终使用 try、catch block 包围像 Integer.parseInt() 这样的调用。

希望对您有所帮助。

关于android - 按下退格键时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14542620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com