This little thing started to upset me a lot. I thought it was something similar, but I think I did not understand how the android works.
In any case, the problem is that my Android application will have some buttons that will open the date or time dialog box when pressed. I know how to implement them directly in the same class as the main activity itself, but I do not think that this is a good solution for maintenance reasons, and also if I intend to develop this application again.
I tried to do this in several different ways and failed every time I tried to do it. This left me pretty elusive about what to do and what the problem is.
Here is one way I tried to do this:
The main action (I simplified it and tried to provide only the necessary code, because it is long). This matches the singleton design pattern.
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dateButton = (Button) findViewById(R.id.editDateButton); dateButton.setOnClickListener(this); } @Override public void onClick(View view) { if(view.getId() == R.id.editStartDateButton) { DatePickers.getInstance().setDate(dateButton); } }
Date picker class (extends activity, code is also simplified and all unnecessary things are deleted):
private static DatePickers self = null; public static DatePickers getInstance() { if (null == self) { self = new DatePickers(); } return self; } public void setDate(Button button) { theButtonUsed = button; showDialog(DATE_DIALOG_ID); }
After calling showDialog, the class is similar to the Hello-DatePicker example on the Android developer site.
I also tried to do this to a large extent, as in here , but with differnece, when a dateButton button is pressed, a new action begins, which is the date selection dialog. In this case, the datePicker class was like this:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.datePicker); showDialog(DATE_DIALOG_ID); }
And everything else, as in the Hello-DatePicker example, on the Android developer site. Both of these solutions lead to a problem when my application crashed and was forced to stop working.
I hope you understand what the problem is, and that you can guide me and show what I'm doing wrong. I tried to do this briefly and provide only the necessary information.
Thanks -Z
Edit:
As I mentioned in one of the comments, the real problem was that I just forgot to add new activity to the manifest file. Perhaps there were some other problems that were resolved with the accepted answer. I hope that this will still help anyone who encounters similar problems.