Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么';我的计算器android应用程序有什么问题?_Java_Kotlin - Fatal编程技术网

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));