1

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

2 Answers2

3

You can not do findViewById inside onDateSet() method

You need to do findViewById inside onCreate() method

SAMPLE EXAMPLE

public class MainActivity extends AppCompatActivity {

    // decalre your EditText Global
    EditText edtDate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // do findViewById inside oncraeate
        edtDate = findViewById(R.id.ev_Date);

    }

}

now use inside onDateSet() like this

    // when ever u want to set text in EditText edtDate just use like this
    edtDate.setText(sdf.format(usercalendar.getTime()));
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • I can place it in onCreate () But I want to update it after user selects Date –  Apr 19 '18 at 10:42
  • Its alreday global and that edtDate is already like that ... thing i am trying to do is to update this edittext after user selects particular date –  Apr 19 '18 at 10:50
  • 1
    @riasat you don't need to call findViewById again, but just use that global variable. – KYHSGeekCode Apr 19 '18 at 10:52
  • from method onDateSet() –  Apr 19 '18 at 10:52
  • @KYHS Its not accesible in onDateSet() even after declaring it global –  Apr 19 '18 at 10:55
  • 1
    @riasat share your whole activity code qith question and also code for your picker – AskNilesh Apr 19 '18 at 10:56
  • @riasat this will help u get reslut of datepicker inside activity check this https://stackoverflow.com/questions/10905312/receive-result-from-dialogfragment – AskNilesh Apr 19 '18 at 11:09
  • @riasat check this https://stackoverflow.com/questions/30777753/how-to-get-the-data-from-dialogfragment-to-mainactivity-in-android – AskNilesh Apr 19 '18 at 11:10
  • @riasat and this https://stackoverflow.com/questions/15121373/returning-string-from-dialog-fragment-back-to-activity – AskNilesh Apr 19 '18 at 11:10
  • @riasat and this https://stackoverflow.com/questions/12622742/get-value-from-dialogfragment – AskNilesh Apr 19 '18 at 11:11
  • 2
    This has the same issue as the `findViewById()` call. You can't access any of `MainActivity`'s instance members – variables or methods – from a static nested class. – Mike M. Apr 20 '18 at 00:14
0

You haven't created an object before calling the method, so the non-static method doesn't exist yet.

owaisafaq
  • 523
  • 4
  • 7