Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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;
}
}
like image 434
user2105000 Avatar asked Feb 25 '13 17:02

user2105000


1 Answers

Edit:

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

This will work ...

like image 167
Pragnani Avatar answered Oct 27 '22 18:10

Pragnani