无法理解为什么在运行以下代码时应用程序崩溃

时间:2016-11-15 15:46:33

标签: android

以下是计算器应用的主要活动代码。在调试期间会弹出一条消息,说“不幸的是应用程序已停止”。谁能帮我这个? plzzz无法弄清楚原因 MainActivity

package com.example.anindyaroy.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

EditText First = (EditText)findViewById(R.id.FirstNum);
EditText Second = (EditText)findViewById(R.id.SecondNum);
EditText Operator = (EditText)findViewById(R.id.Operater);
Button b = (Button)findViewById(R.id.button);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b.setOnClickListener(this);
}
public void onClick(View view)
{
    TextView value= (TextView)findViewById(R.id.Value);
    double first = Double.parseDouble(First.getText().toString());
    double second = Double.parseDouble(Second.getText().toString());
    String operator = Operator.getText().toString();
    char c = operator.charAt(0);
    double result=0.0;
    switch(c)
    {
        case '+':
            result = first+second;
            break;
        case '-':
            result = first - second;
            break;
        case '*':
            result = first * second;
            break;
        case '/':
            result = first/second;
            break;
        case '%':
            result = first%second;
            break;
        default:
            value.setText("Invalid");
    }
    value.setText(String.valueOf(result));
}   
}

2 个答案:

答案 0 :(得分:3)

你需要在setContentView(R.layout.your_layout)函数调用之后在onCreate()方法中调用函数findViewById(R.id.your_view)。

您需要这样做是因为findViewById(R.id.your_view)希望在活动创建视图时调用,以便成功找到您需要的视图。

答案 1 :(得分:2)

按如下方式更改课程:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText First;
    EditText Second;
    EditText Operator;
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        First = (EditText) findViewById(R.id.FirstNum);
        Second = (EditText) findViewById(R.id.SecondNum);
        Operator = (EditText) findViewById(R.id.Operater);
        b = (Button) findViewById(R.id.button);

        b.setOnClickListener(this);
    }
    public void onClick(View view) {
        TextView value = (TextView) findViewById(R.id.Value);
        double first = Double.parseDouble(First.getText().toString());
        double second = Double.parseDouble(Second.getText().toString());
        String operator = Operator.getText().toString();
        char c = operator.charAt(0);
        double result = 0.0;
        switch (c) {
            case '+':
                result = first + second;
                break;
            case '-':
                result = first - second;
                break;
            case '*':
                result = first * second;
                break;
            case '/':
                result = first / second;
                break;
            case '%':
                result = first % second;
                break;
            default:
                value.setText("Invalid");
        }
        value.setText(String.valueOf(result));
    }
}

你只能在setContentView(...)之后调用findViewById。