解析异常
我正在使用 EditText 视图创建一个小型计算应用程序,当用户将 EditText 视图留空导致 ParseInt 尝试解析任何内容时,我会遇到运行时异常。我读到我需要在错误发生之前“尝试”并“捕获”它,但我不确定在哪里以及如何执行此操作!
非常感谢任何建议!
这是我的代码:
public class HandlerExamples extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v) {
String a,b,t;
double vis;
EditText txtbox1 = (EditText)findViewById(R.id.A);
EditText txtbox2 = (EditText)findViewById(R.id.B);
EditText txtbox3 = (EditText)findViewById(R.id.t);
TextView tv = (TextView) findViewById(R.id.Answer);
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
t = txtbox3.getText().toString();
vis = ((Integer.parseInt(a)*1) + (Integer.parseInt(b)*2)) / (Double.parseDouble(t));
tv.setText(double.toString(vis));
}
}
非常感谢!
I am creating a small calc app with EditText views and Im running into an runtime exception when the user leaves an EditText view empty causing the ParseInt to try and Parse nothing. Ive read that I need to 'Try' and 'Catch' this error before it occurs, but Im unsure of where and how to do this!
Any advice is much appreciated!
Here is my code:
public class HandlerExamples extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v) {
String a,b,t;
double vis;
EditText txtbox1 = (EditText)findViewById(R.id.A);
EditText txtbox2 = (EditText)findViewById(R.id.B);
EditText txtbox3 = (EditText)findViewById(R.id.t);
TextView tv = (TextView) findViewById(R.id.Answer);
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
t = txtbox3.getText().toString();
vis = ((Integer.parseInt(a)*1) + (Integer.parseInt(b)*2)) / (Double.parseDouble(t));
tv.setText(double.toString(vis));
}
}
Thanks so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以获取视图 ID 以了解单击了哪个小部件。
you can get the view id to know whick widget was clicked.
Changwei Yao 定义了一种执行此操作的方法,但这是大多数 Android 程序员执行此操作的方法(以编程方式),因为它更容易阅读和弄清楚您的小部件正在做什么:
但首先,删除
implements OnClickListener
从您的 Activity 中,因为不需要。执行相同操作的另一种方法是为您希望在单击时执行操作的小部件定义
android:onClick="insert_method_name_here"
。就您而言,在您的 main.xml 中(因为这就是您在 Activity 中使用的内容),您可以编写类似...的内容,然后在您的 Activity 中定义方法
buttonAction(View v)< /code> 和
textAction(View v)
。请注意,这些方法必须是public void
,并且必须采用唯一的参数View v
。(XML 方法的优点之一是,您不必为这些小部件定义
android:id
属性,除非您需要能够在代码中操作它们或从中提取信息(这意味着您需要为EditText
定义一个android:id
,因为您可能需要用户的输入))Changwei Yao defined one way you can do this, but here's the way most Android programmers would do this (programmatically), since it's a little easier to read and figure out what your widgets are doing:
But first, remove the
implements OnClickListener
from your Activity, as it's not needed.Another way to do the same thing is to define
android:onClick="insert_method_name_here"
for the widgets that you want perform an action when clicked. In your case, in your main.xml (since that's what you're using in your Activity), you could write something like...And then, in your Activity, you define the methods
buttonAction(View v)
andtextAction(View v)
. Note that these methods must bepublic void
, and must take the sole argumentView v
.(One advantage of the XML method is that you don't necessarily have to define an
android:id
attribute for these widgets, unless you need to be able to manipulate them or extract information from them in your code (which means you will need to define anandroid:id
for yourEditText
since you'll likely want the user's input))如果您只需要排除空文本字段,那么 hotveryspicy 的解决方案可能是最快的。对于安全的解决方案:捕获 NumberFormatException 将过滤任何无法转换为整数的内容。
If you only need to exclude the empty text field then hotveryspicy's solution is probably the quickest. For a secure solution: catching the NumberFormatException will filter anything that can not be converted to an integer.