Java 什么';我的计算器android应用程序有什么问题?
我正在制作一个像计算器一样工作的应用程序 这是我的代码片段Java 什么';我的计算器android应用程序有什么问题?,java,kotlin,Java,Kotlin,我正在制作一个像计算器一样工作的应用程序 这是我的代码片段 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
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(字符串a);但是这也没有什么帮助
请指导我如何正确地制作我的代码使用属性
inputType:number
或也可以numberDecimal
值inputType
因为默认情况下,edittext将接受所有字符/数字等,但typecast to integer仅适用于数值。更改此行
tvresult.setText("Result: " + result);
作为
在异常中,它表示
NullPointerException
将变量tvresult
与xml对象绑定。错误是什么?定义“不工作”。致命异常:主进程:com.example.ajdeveloper.myapplication,PID:32227 java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'在com.example.ajdeveloper.myapplication.MainActivity$1.onClick(MainActivity.java:40)的空对象引用上,第40行是:second=Integer.valueOf(etsecond.getText().toString());您是否将onCreate
中所示的代码放在Thanx中作为回复Bro。。我很感激。。但它也不起作用不起作用。。。错误在40行保持不变。。40行:second=Integer.valueOf(etsecond.getText().toString());您可以尝试使用Integer.parseInt(etsecond.getText().toString())
第40行,并检查您的xml文件etsecond
是否没有绑定。因此,它正在抛出NullPointerException
tvresult.setText("Result: " + String.valueOf(result));