这是一个简单的安卓应用。它nullpointerexception
在执行时显示。我无法从edittext
. 在这里,我只想从edittext
. 但它显示nullpointerexception
..
如何解决这个问题?
public class Calci extends Activity {
Button add;
String str1;
String str2;
EditText txt1;
EditText txt2;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calci);
add=(Button) findViewById(R.id.button1);
txt1=(EditText) findViewById(R.id.editText1);
str1=txt1.getText().toString();
txt2=(EditText) findViewById(R.id.editText2);
str2=txt2.getText().toString();
tv=(TextView) findViewById(R.id.textView4);
addition();
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addition();
}
});
}
private void addition(){
int res=0;
res=Integer.parseInt(str1)+Integer.parseInt(str2);
tv.setText(String.valueOf(res));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_calci, menu);
return true;
}
}