The intention is that clicking on an EditText will open the datepicker.
Attached screenshot where you see the error + code
import android.app.DatePickerDialog;
import android.icu.util.Calendar;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText fechas;
int dia, mes, year, uno, dos, tres;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fechas = (EditText)findViewById(R.id.fechas);
fechas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
dia = c.get(Calendar.DAY_OF_MONTH);
mes = c.get(Calendar.MONTH);
year = c.get(Calendar.YEAR);
DatePickerDialog d = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
fechas.setText(dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
}
}
,dia,mes,year);
d.show();
}
});
}
}
When you initialize the
DatePickerDialog
, the first parameter must be aContext
and you are passing the instance of the interfaceView.OnClickListener
:You have to send the instance of the context or the activity like this:
Where
NombreActividadActual
is the name of the activity where you are creating theDatePickerDialog
.