我的 Android 简单计算器应用程序有什么问题?

发布于 2024-11-27 21:40:04 字数 1640 浏览 0 评论 0原文

我是 Android 新手,这是我的第一个应用程序,对我来说似乎很好,但每次我按下计算按钮时,它似乎都会意外停止并强制关闭。

package com.test.simplecalc;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button myButton = (Button) findViewById(R.id.myButton);
    final EditText firstNum = (EditText)findViewById(R.id.firstNum);
    final EditText secondNum = (EditText)findViewById(R.id.secondNum);
    final EditText finalNum = (EditText)findViewById(R.id.finalNum);

    myButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            int num1 = 0;

            try {
                num1 = Integer.parseInt(firstNum.getText().toString());
            } catch(NumberFormatException nfe) {
               Toast.makeText(MainActivity.this, "Could not parse" + nfe, Toast.LENGTH_SHORT).show();
            }

            int num2 = 0;

            try {
                num2 = Integer.parseInt(secondNum.getText().toString());
            } catch(NumberFormatException nfe) {
               Toast.makeText(MainActivity.this, "Could not parse" + nfe, Toast.LENGTH_SHORT).show();
            }
            int num3 = num1 + num2;
            finalNum.setText(num3);
        }
    });
}

}

I'm new to android and this is my first application, it seems fine to me but every time I pressed the calculate button it seems to stop unexpectedly and force close.

package com.test.simplecalc;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button myButton = (Button) findViewById(R.id.myButton);
    final EditText firstNum = (EditText)findViewById(R.id.firstNum);
    final EditText secondNum = (EditText)findViewById(R.id.secondNum);
    final EditText finalNum = (EditText)findViewById(R.id.finalNum);

    myButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            int num1 = 0;

            try {
                num1 = Integer.parseInt(firstNum.getText().toString());
            } catch(NumberFormatException nfe) {
               Toast.makeText(MainActivity.this, "Could not parse" + nfe, Toast.LENGTH_SHORT).show();
            }

            int num2 = 0;

            try {
                num2 = Integer.parseInt(secondNum.getText().toString());
            } catch(NumberFormatException nfe) {
               Toast.makeText(MainActivity.this, "Could not parse" + nfe, Toast.LENGTH_SHORT).show();
            }
            int num3 = num1 + num2;
            finalNum.setText(num3);
        }
    });
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小傻瓜 2024-12-04 21:40:04

这两个块之一很可能抛出 NumberFormatException 以外的其他内容。 (我的猜测是对 toString() 的调用中出现 NullPointerException。)

尝试将每种情况下捕获的异常更改为 Exception 并查看这是否揭示了问题。

It's likely that one of the two blocks is throwing something other than NumberFormatException. (My guess would be a NullPointerException in the call to toString().)

Try changing the caught exception in each case to Exception and see if this reveals the problem.

烟织青萝梦 2024-12-04 21:40:04

只是在打印答案时尝试

num3.toString() 

,最好使用 textview 来显示答案

just try

num3.toString() 

while printing the answer, and better to use textview for showing answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文