Hi all,
Still early days on my Android development journey but I'm wanting to streamline the creation of DatePickers as outlined in the "Hello, Date Picker" tutorial (link to tutorial). I've created a class which extends Activity that accepts a few layout references in a bundle and is then supposed to handle the DatePickers. I then start an activity based on this class from my main .java file (with an intent and bundle which includes references to layout ids in the main.xml file).
As you might imagine this doesn't work at all - I'm presented with an infinite loop of runtime errors which states:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.master/com.example.master.EasyDatePick}
I've checked the AndroidManifest.xml file and I do have a reference to the EasyDatePick Activity class I created - so that probably isn't the problem.
<activity android:name=".EasyDatePick" android:label="@string/app_name"/>
My EasyDatePick class is nothing ground breaking - I've based it off the Android Developers tutorial. Full source is attached below:
public class EasyDatePick extends Activity {
// Date picker variables
private EditText mDateDisplay;
private ImageButton mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
private final Context mCtx;
// Requires a bundle with layoutId, editTextId and pickDateId specified
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
// extract extras from bundle
int layoutId = bundle.getInt("layoutId");
int editTextId = bundle.getInt("editTextId");
int pickDateId = bundle.getInt("pickDateId");
// set the content view to focus on
setContentView(layoutId);
// set the edit text and button to use
mDateDisplay = (EditText) findViewById(editTextId);
mPickDate = (ImageButton) findViewById(pickDateId);
// add a click listener to the button
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date (this method is below)
updateDateDisplay();
}
public EasyDatePick(Context ctx) {
this.mCtx = ctx;
}
// updates the date in the TextView
private void updateDateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/")
.append(mDay).append("/")
.append(mYear).append(" "));
}
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
}
My main .java file calls are:
Intent intent = new Intent().setClass(this, EasyDatePick.class);
Bundle bundle = new Bundle();
//Add the parameters to bundle
bundle.putInt("layoutId", R.layout.main);
bundle.putInt("editTextId", R.id.dateField);
bundle.putInt("pickDateId", R.id.dateButton);
intent.putExtras(bundle);
startActivity(intent);
I have a feeling I'm way off track in what I'm doing and probably shouldn't be spawning activities to handle each DatePicker. Any assistance would be greatly appreciated.