NullPointerException on findViewById() in android

人盡茶涼 提交于 2019-11-28 01:11:35

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

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);
} 

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!