how to setText from value of DatePickerDialog

839 views Asked by At

Im trying to pick date after clicking on button and setting its value on TextView. This thread helped Datepicker: How to popup datepicker when click on button and store value in variable

But im unable to set value of datepicker on setText,the textview object v returns empty nullpointerexception.

         v.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("/").append(mDay).append("/")
                .append(mYear).append(" "));
        System.out.println(v.getText().toString());

Any help is appreciated

Full code: Mainactivity.java

     public class MainActivity extends Activity   {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}



public void pick(View v){

    Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);
    System.out.println("the selected " + mDay);
    DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,
            new mDateSetListener(), mYear, mMonth, mDay);
    dialog.show();

    }

}



     class mDateSetListener  implements DatePickerDialog.OnDateSetListener {

private TextView v;

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    // TODO Auto-generated method stub
    // getCalender();
    int mYear = year;
    int mMonth = monthOfYear;
    int mDay = dayOfMonth;
    v.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(mMonth + 1).append("/").append(mDay).append("/")
            .append(mYear).append(" "));



}
}

xml file:

     <RelativeLayout  
    android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.datesamp.MainActivity" >


    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_marginTop="101dp"
    android:text="Pick date" 
    android:onClick="pick"/>

   <TextView
   android:id="@+id/t1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
     />






     </RelativeLayout>
1

There are 1 answers

0
Livio On BEST ANSWER

You need to initialize the variable v before using it in onDateSet(), e.g. adding the following statement before v.setText(...):

v = (TextView) findViewById(R.id.t1);
v.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(mMonth + 1).append("/").append(mDay).append("/")
            .append(mYear).append(" "));

But in order to insert this statement inside onDateSet(), I suggest you to define the class mDateSetListener as an inner class inside MainActivity, it will make your life easier.