Trying to develop a basic calculator in android studio. Error: "Unfortunately, Calculator has stopped"

Trying to develop a basic calculator in android studio. Error: "Unfortunately, Calculator has stopped"

java代码:

package com.kumar.calc;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import java.util.Scanner;



public class Calc extends ActionBarActivity {

    private EditText number1;
    private EditText number2;
    private TextView result = (TextView)findViewById(R.id.result);
    private int a,b;
    private float res;

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


        number1 = (EditText)findViewById(R.id.number1);
        a=Integer.parseInt(number1.getText().toString());

        number2 = (EditText)findViewById(R.id.number2);
        b=Integer.parseInt(number2.getText().toString());

        Button addButton = (Button)findViewById(R.id.addButton);
        addButton.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View v){
                        res=a+b;
                        result.setText("=" + res);
                    }
                }
        );

        Button subtractButton = (Button)findViewById(R.id.subtractButton);
        subtractButton.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View v){
                        res=a-b;
                        result.setText("=" + res);
                    }
                }
        );

        Button multiplyButton = (Button)findViewById(R.id.multiplyButton);
        multiplyButton.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View v){
                        res=a*b;
                        result.setText("=" + res);
                    }
                }
        );

        Button divideButton = (Button)findViewById(R.id.divideButton);
        divideButton.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View v){
                        res=(float)a/b;
                        result.setText("=" + res);
                    }
                }
        );


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_calc, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

现在在 xml 如果它产生错误,字符串资源不能有空值 如果我在@+id/之外写任何仲裁词 然后在模拟器中,它指出,不幸的是计算器已停止 XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".Calc"
                android:id="@+id/relativeLayout">

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/number1"
        android:layout_marginTop="20dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/number2"
        android:layout_marginTop="21dp"
        android:layout_below="@+id/number1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="@string/ButtonOfAddition"
        android:id="@+id/addButton"
        android:layout_below="@+id/number2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="34dp"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="@string/ButtonOfSubtraction"
        android:id="@+id/subtractButton"
        android:layout_alignBottom="@+id/addButton"
        android:layout_toRightOf="@+id/addButton"
        android:layout_toEndOf="@+id/addButton"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="@string/ButtonOfMultiplication"
        android:id="@+id/multiplyButton"
        android:layout_alignBottom="@+id/subtractButton"
        android:layout_toRightOf="@+id/subtractButton"
        android:layout_toEndOf="@+id/subtractButton"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="@string/ButtonOfDivision"
        android:id="@+id/divideButton"
        android:layout_alignBottom="@+id/multiplyButton"
        android:layout_toRightOf="@+id/multiplyButton"
        android:layout_toEndOf="@+id/multiplyButton"/>

     <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/ResultField"
        android:id="@+id/result"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/multiplyButton"
        android:layout_toStartOf="@+id/multiplyButton"/>
</RelativeLayout>

这是错误的:

 number1 = (EditText)findViewById(R.id.number1);
 a=Integer.parseInt(number1.getText().toString());

 number2 = (EditText)findViewById(R.id.number2);
 b=Integer.parseInt(number2.getText().toString());

您在 onCreate 中调用它。 您应该在按钮的 onClickListener 中执行此操作,如果文本为空(您正在调用 number2.getText().toString(),并且我认为 toString() 正在崩溃)您将得到一个异常。