I have code:
public class HelloWorld extends Activity {
private Button buttonOk;
private Button buttonCancel;
private OnClickListener buttonOkListener = ne
I was having the same trouble, but after cleaning my project and running it again it works perfectly.
At times there are few points to be considered!
This can always happen even for experienced developers as well. I would suggest few points below,
findViewById
is added in the appropriate life cycle of the activity or fragment which ever under consideration.
setContentView
- which was incorrect in my case.I'm not 100% sure but you are calling the findviewbyid's in the class initialisation. I think this code is called before the onCreate method so the view's cannot be found. Initializing the listeners in the oncreate method should work.
there is no problem with your codes, by right everything should work as per normal.
The most common mistake of encountering null via findViewById()
method is when you forgot to call setContentView()
or called it for the wrong layout.
I suggest cleaning your project and try again!!!!