0

I want the date selected by the date picker to displayed on the button which invokes the date picker (through a fragment class). I tried to do this by making a method in the class which defines the action to be taken after the onClick of the button. The method changes the text via setText(). This method is called by the fragment class through the onDateSet() button.

Now when I set the date through the Date picker, the app crashes.

Code:1.

  public void onDateSet(DatePicker view, int year, int month, int day) {
      Addproject changetxt = new Addproject();
      changetxt.changebuttontext(year, month, day);
      //Doing something with the date chosen
    }

Here the method, which will change the text of the button, from the other class is called and the day, month, year data is passed.

Code 2.

    public void changebuttontext (int year, int month, int day) {
        Button date_button = (Button)findViewById(R.id.datebutton);     
        date_button.setText(year + "/" + month + "/" + day);
    }

This method is supposed to change the text on the button.

CatLog:

12-24 22:42:25.608: E/AndroidRuntime(3478):   at     
com.kk.project.DatePickerFragment.onDateSet(DatePickerFragment.java:33)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at     
android.app.DatePickerDialog.onClick(DatePickerDialog.java:111)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at  
com.android.internal.app.AlertController$ButtonHandler.handleMessage
(AlertController.java:166)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at  
android.os.Handler.dispatchMessage(Handler.java:99)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at      
android.os.Looper.loop(Looper.java:137)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at     
android.app.ActivityThread.main(ActivityThread.java:4424)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at  
java.lang.reflect.Method.invokeNative(Native Method)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at 
java.lang.reflect.Method.invoke(Method.java:511)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at     
dalvik.system.NativeStart.main(Native Method)

I have only posted the part which was in red

krtkush
  • 1,378
  • 4
  • 23
  • 46

2 Answers2

0
public void changebuttontext (int year, int month, int day) {
    Button date_button = (Button)findViewById(R.id.datebutton);     
    date_button.setText(year + "/" + month + "/" + day);
}

This code should be called from the Activity class. if AddProject is an external class program will crash.

  • I can't! onDataSet is part of a fragment class; `public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener` and findViewById is not recognized by such classes. – krtkush Dec 24 '12 at 17:46
0

The problem I think after looking at your data is, you are trying to findViewById when the activity is not in focus. As a solution you can try something like this.

public void changebuttontext (int year, int month, int day) {
   selectedDate = year + "/" + month + "/" + day;
}

here selected date is a field variable. Now in onWindowFocusChange() or onResume whichever works place the

 Button date_button = (Button)findViewById(R.id.datebutton);     
    date_button.setText(selectedDate);

part.

might want to check this for the excetion. Illegal argument exception in android dialog

Community
  • 1
  • 1
Hardik4560
  • 3,202
  • 1
  • 20
  • 31
  • Not working. The same problem still persists. I added `Button date_button = (Button)findViewById(R.id.datebutton); date_button.setText(selectedDate);` to the same class, under `onWindowFocusChange()`, which has the `changebuttontext()`. If I use onResume, the app would crash as soon as I would enter the activity which contains the button. – krtkush Dec 24 '12 at 18:11
  • I posted the question again but with a different approach. Got the answer. [here](http://stackoverflow.com/questions/14024921/retrieve-and-set-data-from-dialogfragment-datepicker) is the link. Thank you for your time, though :) – krtkush Dec 24 '12 at 20:27
  • got the answer...wasn't that a simple solution.. :) – Hardik4560 Dec 25 '12 at 10:58