NullPointerException on findViewById() in android?

The setContentView method should be called with appropriate layout before calling findViewById It is usually called in onCreate(Bundle savedInstance) method.

The setContentView method should be called with appropriate layout before calling findViewById. It is usually called in onCreate(Bundle savedInstance) method.

3 Its important to note: there is honestly very little (to be precise, there is NOTHING) user-interface related that you can safely do in the constructor of an Activity. In general, you should use the onCreate method for everything you would normally use the constructor for. – Femi May 22 at 19:30 @Femi - That's an important point.

Thanks for pointing that out. – MByD May 22 at 19:32 Thanks, I think I got it. Was a huge migration of code to fix it haha.

– pointour May 22 at 19:40 1 The reason for this is that Activity#findViewById isn't magical, it's a simple recursive search down the view hierarchy attached to an activity's current window. If you haven't attached any views by using setContentView or similar yet, findViewById will return null because there were no matching views present to find. – adamp May 22 at 21:07.

You have to call it from your Activity's onCreate method, as the resources will not have been made available before that point. So expanding MByD's answer, in your onCreate method, first call setContentView(), then findViewById().

First , you should call the setContentView(int layout) ,in order to set the Content of your Activity , and then you can get your Views ( findViewById(int id) ) ; So your Activity will be like this : public class UserInteraction extends Activity { EditText etFrom; int from; EditText etTill; int till; public void onCreate(Bundle savedInstance{ super. OnCreate(saveInstance); this. SetContentView(R.layout.

Main); etFrom = (EditText)findViewById(R.id. Et_from); etTill = (EditText)findViewById(R.id. Et_till); } }.

EtFrom = (EditText)findViewById(R.id. EtTill = (EditText)findViewById(R.id. Int I = Integer.

Int I = Integer.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions