Android DatePickerDialog crash on 2.3-Collection of common programming errors
I have a DatePickerDialog on my application. I am using DatePickerDialog because I need to show only month and year and it’s easier with dialog than datepicker. Its working fine in 4.0 or higher version but when I try to open the DatePickerDialog clicking an edittext in 2.3 the app get crashed.
Here the code:
import java.lang.reflect.Field;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.lang.reflect.Field;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.support.v4.app.DialogFragment;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
public class ElegirMesAnoActivity extends Activity {
CustomDatePicker dpFecha;
Button btnAceptar, btnVolver;
private int mYear;
private int mMonth;
private int mDay;
private EditText etPickADate;
static final int DATE_DIALOG_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_elegir_mes_ano);
btnAceptar=(Button)findViewById(R.id.btnAceptar);
btnVolver=(Button)findViewById(R.id.btnVolver);
//dpFecha=(CustomDatePicker)findViewById(R.id.dpFechaGrafico);
etPickADate = (EditText) findViewById(R.id.et_datePicker);
etPickADate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
btnAceptar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Date fecha= new Date(mYear-1900, mMonth, mDay);
//Date fecha=new Date(dpFecha.getYear()-1900, dpFecha.getMonth(), dpFecha.getDayOfMonth());
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("dd/MM/yyyy");
String fechaStr = sdf.format(fecha);
Intent intent =
new Intent(ElegirMesAnoActivity.this, EstadisticaPorDiaActivity.class);
Bundle b = new Bundle();
b.putString("fecha", fechaStr);
intent.putExtras(b);
startActivity(intent);
}
});
btnVolver.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
DatePickerDialog.OnDateSetListener mDateSetListner = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDate();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
/*
* return new DatePickerDialog(this, mDateSetListner, mYear, mMonth,
* mDay);
*/
DatePickerDialog datePickerDialog = this.customDatePicker();
return datePickerDialog;
}
return null;
}
protected void updateDate() {
int localMonth = (mMonth + 1);
String monthString = localMonth < 10 ? "0" + localMonth : Integer
.toString(localMonth);
String localYear = Integer.toString(mYear).substring(2);
etPickADate.setText(new StringBuilder()
// Month is 0 based so add 1
.append(monthString).append("/").append(localYear).append(" "));
showDialog(DATE_DIALOG_ID);
}
private DatePickerDialog customDatePicker() {
DatePickerDialog dpd = new DatePickerDialog(this, mDateSetListner,
mYear, mMonth, mDay);
try {
Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
for (Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField
.get(dpd);
Field datePickerFields[] = datePickerDialogField.getType()
.getDeclaredFields();
for (Field datePickerField : datePickerFields) {
if ("mDayPicker".equals(datePickerField.getName())
|| "mDaySpinner".equals(datePickerField
.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
} catch (Exception ex) {
}
return dpd;
}
}