I am using (link below) Date picker to select the date from the user and after selection i want to update my TextBox (Edit Text) but it is giving me "Non static method findviewbyid int cannot be referenced from a static context" error Can any body suggest me any other way of doing the same thing or any error in this method?
https://developer.android.com/guide/topics/ui/controls/pickers.html
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
String myFormat = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
((EditText) findViewById(R.id.ev_Date)).setText(sdf.format(usercalendar.getTime()));
}
Thats my whole code
public class CreateReport extends AppCompatActivity {
public EditText EventDate;
private static Calendar usercalendar;
private String Lat, Long;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_report);
// final ActionBar ab = getSupportActionBar();
// ab.setTitle("Create Reoprt");
Bundle bundle = getIntent().getExtras();
EventDate = (EditText) findViewById(R.id.ev_Date);
if (bundle != null) {
Lat = bundle.getString("lat");
Toast.makeText(getContext(), "Latitude" + Lat, Toast.LENGTH_LONG).show();
Long = bundle.getString("Long");
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
String myFormat = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
// ((EditText) findViewById(R.id.ev_Date)).setText(sdf.format(usercalendar.getTime()));
// EventDate.setText(sdf.format(usercalendar.getTime()));
//Unaccesable
}
}