NullPointerException on findViewById() in android

前端 未结 3 1613
我在风中等你
我在风中等你 2020-12-06 18:35

In the following code i get a NullPointerException on lines 9/10 with findViewById().
In my main class I just instantiated an object from this class, to use .getFrom()

3条回答
  •  一生所求
    2020-12-06 19:16

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

    }

提交回复
热议问题