I have only been developing Android for two weeks now and have done lots of googling into this problem I have but can’t seem to get anything to work.
I have a Helper Class which has one method to set up DatePicker Limits etc.
public class AlarmHelper extends Activity {
public void setupDatePicker(){
long maxTime = 52560000;
final Calendar cal = Calendar.getInstance();
DatePicker dp = (DatePicker)findViewById(R.id.datePicker);
dp.setMinDate(cal.getTimeInMillis() - 1000);
dp.setMaxDate(cal.getTimeInMillis() + maxTime);
}
}
I then try to call the setupDatePicker() from the MainActivity in the onCreate() method like so.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
AlarmHelper helper = new AlarmHelper();
helper.setupDatePicker();
setContentView(R.layout.activity_main);
}
When I run the app I get a NullPointerException. I believe it has something to do the the Context but I haven’t got a clue how to implement it within my helper class.
Any help would be greatly appreciated.
currently you are trying to
findViewByIdbefore setting Activity layout .change your ActivityonCreatecode as :and in your AlarmHelper no need to extends Activity class if it’s not an Activity .Change your
AlarmHelperclass as because to access UI elements from other non Activity class you will need to pass activity context :