This is a simple app in android. It shows nullpointerexception while executing.
I can’t get the value from the edittext. Here I just want to get the value from the edittext.
But it shows nullpointerexception..
how to solve this?
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;
}
}
make sure you are setting default values for
editText1andeditText2in xml or if you want to access values enter by user then chnage your code as and remove addition() before setOnClickListener :