Reputation: 727
I have a strange error, I get NullPointerException error when I set text to EditText. Code is something like below:
EditText editTxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editTxt = (EditText) findViewById(R.id.edtTxt);
if(someCondition) {
if (editTxt!=null)
editTxt.setText("HelloWorld");
}
}
}
Upvotes: 0
Views: 1162
Reputation: 727
//Got it working like this,
String hello = "HelloWorld!";
EditText editTxt;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editTxt = (EditText)findViewById(R.id.edtTxt);
if(someCondition) {
if(editTxt!=null) editTxt.setText(hello);
}
}
Upvotes: 0
Reputation: 875
Please try this code:
EditText editTxt = (EditText) findViewById(R.id.text);
if (editTxt!=null) {
editTxt.setText("HelloWorld");
}
Upvotes: 0
Reputation: 11975
in this small code,There should be one Error only
editTxt = (EditText)findViewById(R.id.edtTxt);
that your id in xml doesnot match with edtTxt.If yes then try to clean it once then run
Upvotes: 1
Reputation: 309008
The object that's cited in the stack trace wasn't initialized. You never asked it to point to a new object on the heap.
I'm putting my money on that reference R
. I don't see where that's initialized.
Upvotes: 0