解析异常

发布于 2024-11-28 04:22:54 字数 1233 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剪不断理还乱 2024-12-05 04:22:54
public void onClick(View v) {
    int id = v.getId();
    switch(id){
    case R.id.xx:
        //do things xx click
        break;
    case R.id.yy:
        //do things yy click
        break;
    }
}

您可以获取视图 ID 以了解单击了哪个小部件。

public void onClick(View v) {
    int id = v.getId();
    switch(id){
    case R.id.xx:
        //do things xx click
        break;
    case R.id.yy:
        //do things yy click
        break;
    }
}

you can get the view id to know whick widget was clicked.

只想待在家 2024-12-05 04:22:54

Changwei Yao 定义了一种执行此操作的方法,但这是大多数 Android 程序员执行此操作的方法(以编程方式),因为它更容易阅读和弄清楚您的小部件正在做什么:

但首先,删除 implements OnClickListener 从您的 Activity 中,因为不需要。

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // what you want your button to do when clicked
    }
}
editText.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // what you want your EditText to do when clicked
        // (such as editText.setText(""))
    }
}

执行相同操作的另一种方法是为您希望在单击时执行操作的小部件定义 android:onClick="insert_method_name_here"。就您而言,在您的 main.xml 中(因为这就是您在 Activity 中使用的内容),您可以编写类似...的内容

<Button android:id="@+id/testButton"
    (other attributes you wish to apply to the button)
    android:onClick="buttonAction" />
<EditText
    (other attributes)
    android:onClick="textAction" />

,然后在您的 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.

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // what you want your button to do when clicked
    }
}
editText.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // what you want your EditText to do when clicked
        // (such as editText.setText(""))
    }
}

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...

<Button android:id="@+id/testButton"
    (other attributes you wish to apply to the button)
    android:onClick="buttonAction" />
<EditText
    (other attributes)
    android:onClick="textAction" />

And then, in your Activity, you define the methods buttonAction(View v) and textAction(View v). Note that these methods must be public void, and must take the sole argument View 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 an android:id for your EditText since you'll likely want the user's input))

萌辣 2024-12-05 04:22:54

如果您只需要排除空文本字段,那么 hotveryspicy 的解决方案可能是最快的。对于安全的解决方案:捕获 NumberFormatException 将过滤任何无法转换为整数的内容。

int vis;
try {
    vis = Integer.parseInt(a);
}
catch(NumberFormatException nfe) {
    Log.e(TAG,"trying to convert:"+a+" to integer failed");
    vis = 0;
}

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.

int vis;
try {
    vis = Integer.parseInt(a);
}
catch(NumberFormatException nfe) {
    Log.e(TAG,"trying to convert:"+a+" to integer failed");
    vis = 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文