Search code examples
androidnon-staticdatepickerdialog

error: non-static method show() cannot be referenced from a static context DatePickerDialog.show();


I am developing a date-picker dialog in my application by using this tutorial (youtube link https://www.youtube.com/watch?v=AdTzD96AhE0), in which I am taking the input from the user by using date-text by applying date-picker dialog, and it is also displayed on the same editText(editText id "editTextDate" ). I have an error message that the non-static method show() cannot be referenced from a static context DatePickerDialog.show();\ How can I do this????

This is my all code

public class Match_Details extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

    private TextView dateText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match__details);
        dateText = findViewById(R.id.editTextDate);

        findViewById(R.id.editTextDate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View V) {
            }
        });
    }
    private void showDatePickerDailog(){
            DatePickerDialog datePickerDialog= new DatePickerDialog(
                    this,
                    this,
                    Calendar.getInstance().get(Calendar.YEAR),
                    Calendar.getInstance().get(Calendar.MONTH),
                    Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
            );
            DatePickerDialog.show();
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        String date= dayOfMonth +"/"+ month +"/"+ year;
        dateText.setText(date);
    }
} 

Part of the XML Code

<EditText
        android:id="@+id/editTextDate"
        android:layout_width="158dp"
        android:layout_height="46dp"
        android:layout_marginTop="180dp"
        android:autoLink="phone"
        android:ems="10"
        android:hint="@string/date"
        android:inputType="date"
        android:textAlignment="center"
        app:layout_constraintEnd_toStartOf="@+id/editTextTime"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button" />

Solution

  • datePickerDialog.show();
    

    you should refernce it to an instance, not Class.