【问题标题】:How to add integers and compare the result with number如何添加整数并将结果与​​数字进行比较
【发布时间】:2014-03-09 22:46:47
【问题描述】:

我有以下代码:

public class MainActivity extends Activity {

    int a1_val = 0;
    int b1_val = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goto_start);

        Button a1_btn =(Button)findViewById(R.id.a1_btn);
        Button b1_btn =(Button)findViewById(R.id.b1_btn);
        Button result_btn  =(Button)findViewById(R.id.result_btn);

        // a1 click
        a1_btn.setOnClickListener(new OnClickListener() {
            @Override
        public void onClick(View v) {
                a1_val = 1;
            };
        });

        // b1 click
        b1_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                b1_val = 5;;
            };
        });

        result_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if { (a1_val + b1_val == 6);
                    View startpage = (View)findViewById(R.id.startpage);
                    startpage.setVisibility(View.VISIBLE);
            }; 
            else break;

为什么当我单击 result_btn 时应用程序崩溃?我想我在if语句中犯了一个错误,但我不知道问题是什么。

【问题讨论】:

  • 你尝试了什么?你只是发布一些随机代码,显示一些按钮......
  • 对不起,我现在编辑了,我试过了!
  • 你能把这段代码产生的错误贴出来吗?

标签: android eclipse math integer logic


【解决方案1】:

我看到的问题是 IF 语句的括号,这是解决方法:

if (a1_val + b1_val == 6) {
    View startpage = (View) findViewById(R.id.startpage);
    startpage.setVisibility(View.VISIBLE);
}

而且我认为这不是这里唯一的问题......

编辑 - 这是重写的整个发布的代码:

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goto_start);

        final Button a1_btn = (Button) findViewById(R.id.a1_btn);
        final Button b1_btn = (Button) findViewById(R.id.b1_btn);
        Button result_btn = (Button) findViewById(R.id.result_btn);

        // a1 click
        a1_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setText(1);
            }
        });

        // b1 click
        b1_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setText(5);
            }
        });

        result_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((Integer.parseInt(a1_btn.getText()) + Integer.parseInt(b1_btn.getText())) == 6) {
                    View startpage = (View) findViewById(R.id.startpage);
                    startpage.setVisibility(View.VISIBLE);
                }
            });
        }
    // ... other code of Activity

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-05
    • 2016-01-16
    • 1970-01-01
    • 2012-05-12
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多