我正在製作一個像計算器一樣的應用程序..我的計算器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的形式);但它也沒有幫助..
請指導我如何使我的正常
什麼是錯誤? – user3808887
定義「不工作」。 – John3136
致命例外:main 進程:com.example.ajdeveloper.myapplication,PID:32227 java.lang.NullPointerException:嘗試調用虛擬方法'void android.widget.TextView.setText(java.lang.CharSequence)'on a null對象引用 at com.example.ajdeveloper.myapplication.MainActivity $ 1.onClick(MainActivity.java:40) –