Home » Android » android – Error: "Cannot make a static reference to the non-static method findViewById(int) from the type Activity"

android – Error: "Cannot make a static reference to the non-static method findViewById(int) from the type Activity"

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m new to Java, and trying to create an activity with an EditText. On creation, it loads with the current date and when the user selects the EditText, it display a DatePicker. Once the user selects the date, I need to put the result into the EditText. However, I’m currently getting the following error:

Cannot make a static reference to the non-static method findViewById(int) from the type Activity

I know that I cannot make a static reference to a non-static method. I’ve tried to remove all static references, but this gave me other errors. My code is below.

Can you help me with some sample code of how I can make this work? The error is at where when I try to put the result into the EditText.

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Get Current Date and assign to EditText Begin
    Calendar c = Calendar.getInstance();
    System.out.println("Current time => " + c.getTime());

    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
    String formattedDate = df.format(c.getTime());

    EditText ShowDate = (EditText) findViewById(R.id.editTextToShowDate);
    ShowDate.setText(formattedDate);
    //Get Current Date and assign to EditText End
}

//Date Picker Start
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}    

@TargetApi(Build.VERSION_CODES.HONEYCOMB) public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

@TargetApi(Build.VERSION_CODES.HONEYCOMB) @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 sToDate = createStringFromDateElements(year, month, day);
    EditText ShowDate = (EditText) findViewById(R.id.editTextToShowDate);  //I get the error here
    ShowDate.setText(sToDate);  
}

private String createStringFromDateElements(int year, int month, int day) {
    // TODO Auto-generated method stub
    return null;
}
}
//Date Picker End

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}
How to&Answers:

Edit:

EditText ShowDate = (EditText)getActivity(). findViewById(R.id.editTextToShowDate);  

This will work …

Answer:

Inside Activity class, i have made editText as static and

EditText ShowDate = (EditText)this.findViewById(R.id.editTextToShowDate);

It worked for me.

For more here