在android中使用异常
尊重所有人;
我是编程新手,发现一个障碍,即单击按钮而不在编辑文本框中输入任何内容会将我的活动转换为崩溃。
因此,经过研究,我得到了 Try and catch 方法,效果很好。
public void clickDiv(View button){
try{
EditText Input = (EditText) findViewById(R.id.editext);
String input = Input.getText().toString();
String empty = "";
Float floatInput = new Float (input);
TextView TextShow = (TextView) findViewById(R.id.textView1);
String Newinput = floatInput.toString();
TextShow.setText(Newinput);
if (answer == 0){
answer = (answer+1) / floatInput ;
}else{
answer = (answer) / floatInput ;
}
String answerString = answer.toString();
TextShow.setText(answerString);
Input.setText(empty); }
catch (Exception e) {
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage("Could not find the operand");
alertDialog.show();
}}
但主要问题是我必须在所有按钮方法中使用它。 还有其他方法可以避免代码中的这种重复吗?
请帮忙..
respected all;
I am new to programming and find a barrier i.e on clicking the button without any thing being inputted in the Edittext box converts mine activity to crash.
So after research i got Try and catch methods and it works good .
public void clickDiv(View button){
try{
EditText Input = (EditText) findViewById(R.id.editext);
String input = Input.getText().toString();
String empty = "";
Float floatInput = new Float (input);
TextView TextShow = (TextView) findViewById(R.id.textView1);
String Newinput = floatInput.toString();
TextShow.setText(Newinput);
if (answer == 0){
answer = (answer+1) / floatInput ;
}else{
answer = (answer) / floatInput ;
}
String answerString = answer.toString();
TextShow.setText(answerString);
Input.setText(empty); }
catch (Exception e) {
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage("Could not find the operand");
alertDialog.show();
}}
But the main problem is that i have to use it in all the button methods.
is there any other way other to avoid this repetition in code.
Please help..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样修改代码。希望对您有帮助。
modify the code like this. Hope it helps you.