致命异常:主要在我的补充计划中

时间:2016-07-04 16:52:24

标签: java android

我的简单添加程序正在运行并按预期添加。但是,我担心我从logcat读取的以下日志:

07-04 12:32:57.566 3714-3714/com.example.shirjil.rouletteheatmap E/AndroidRuntime: FATAL EXCEPTION: main

我的.java代码:

package com.example.shirjil.rouletteheatmap;

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

public class MainActivity extends AppCompatActivity {

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

public void onButtonClick(View v)
{
    int n1, n2, sum;
    EditText e1 = (EditText)findViewById(R.id.num1);
    EditText e2 = (EditText)findViewById(R.id.num2);
    TextView t = (TextView)findViewById(R.id.sum);
    n1 = Integer.parseInt(e1.getText().toString());
    n2 = Integer.parseInt(e2.getText().toString());
    sum = n1 + n2;
    t.setText(Integer.toString(sum));

    }
}

我是java和Android Studio的新手。谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

试试这个:

public class MainActivity extends AppCompatActivity {

EditText e1;
EditText e2;
TextView t;

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

    e1 = (EditText)findViewById(R.id.num1);
    e2 = (EditText)findViewById(R.id.num2);
    t = (TextView)findViewById(R.id.sum);
}

public void onButtonClick(View v)
{
    int n1, n2, sum;
    n1 = Integer.parseInt(e1.getText().toString());
    n2 = Integer.parseInt(e2.getText().toString());
    sum = n1 + n2;
    t.setText(String.valueOf(sum));

    }
}