我的计算器android应用程序有什么问题?

我正在制作一个像计算器一样的应用程序。

这是我的代码片段。

EditText etfirst,etsecond; Button btnadd; etfirst = (EditText)findViewById(R.id.etfirst); etsecond = (EditText)findViewById(R.id.etsecond); btnadd = (Button)findViewById(R.id.btnadd); btnadd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int first,second,result; first= Integer.valueOf(etfirst.getText().toString()); second = Integer.valueOf(etsecond.getText().toString()); result = first + second; tvresult.setText("Result: " + result); } }); 

我的问题是,当我从EditText到TextView的整数值它不工作..

我也试过Integer.parseInt(String a); 但它也是没有帮助的..

请指导我如何使我的代码正确

使用属性inputType:number或者也可以是inputType numberDecimal值。

因为默认情况下edittext将接受所有的字符/数字等,但是只能转换为整数才能在数值上工作。

改变这一行

 tvresult.setText("Result: " + result); 

 tvresult.setText("Result: " + String.valueOf(result)); 

在例外情况下,它说NullPointerException绑定变量tvresult与您的xml对象。