Search code examples
settext

Second Datebtn.setText() inside switch case does not settext the selected date value & Datebtn text continues to show only the current date


I have tried this button.set() in multiple ways by doing this inside dateonset () method and I get a null pointer exception there though the editText4 contains the valid text to set the button. I tried the route of Shared preferences as I suspected the edittext4 is empty. Finally I tried this method to initialize the Datebtn inside the Onclick method from view. Here I don't get a null pointer exception but Datebtn.setText() inside switch case does not settext the selected date value & Datebtn text continues to show only the current date that was set in the onCreateView () method. I think I am missing something trivial.

public class AFragment extends Fragment implements OnClickListener {

    private DataManipulator dh;
    public Button           Datebtn;
    private Calendar        mCalen;
    private int             day;
    private int             month;
    private int             year;
    public String           myEditText4;

    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
        View view = inflater.inflate( R.layout.afragment, container, false );
        Button b1 = (Button)view.findViewById( R.id.Button01 );
        b1.setOnClickListener( this );
        Button Datebtn = (Button)view.findViewById( R.id.datepickerbutton );
        mCalen = Calendar.getInstance();
        day = mCalen.get( Calendar.DAY_OF_MONTH );
        month = mCalen.get( Calendar.MONTH );
        year = mCalen.get( Calendar.YEAR );
        Datebtn.setText( day + " / " + (month + 1) + " / " + year );
        myEditText4 = (String)Datebtn.getText();
        Datebtn.setOnClickListener( this );
        return view;
    }

    @SuppressLint( "CutPasteId" )
    @Override
    public void onClick( View view ) {
        Context context = view.getContext();
        Button Datebtn = (Button)view.findViewById( R.id.datepickerbutton );

        switch( view.getId() ) {
            case (R.id.datepickerbutton):
                DatePickerFragment newDateFragment = new DatePickerFragment();
                newDateFragment.setStyle( 1, 1 );
                newDateFragment.show( getFragmentManager(), "DatePicker" );
                Datebtn.setText( myEditText4 );
                break;

            case (R.id.Button01):
                View editText1 = (EditText)getView().findViewById( R.id.ppl );
                View editText2 = (EditText)getView().findViewById( R.id.litres );
                View editText3 = (EditText)getView().findViewById( R.id.odo );
                String myEditText1 = ((TextView)editText1).getText().toString();
                String myEditText2 = ((TextView)editText2).getText().toString();
                String myEditText3 = ((TextView)editText3).getText().toString();
                dh = new DataManipulator( context );
                if( myEditText1 != "" & myEditText2 != "" & myEditText3 != "" & myEditText4 != "" ) {
                    dh.insert( myEditText1, myEditText2, myEditText3, myEditText4 );
                    Toast.makeText( context.getApplicationContext(), "Submit Successful!!!", Toast.LENGTH_LONG ).show();
                    EditText text1 = (EditText)getView().findViewById( R.id.ppl );
                    text1.setText( "" );
                    EditText text2 = (EditText)getView().findViewById( R.id.litres );
                    text2.setText( "" );
                    EditText text3 = (EditText)getView().findViewById( R.id.odo );
                    text3.setText( "" );
                } else {
                    Toast.makeText( context.getApplicationContext(), "Values can not be empty!!!", Toast.LENGTH_LONG ).show();
                }
                break;
        }
    }

    @SuppressLint( "ValidFragment" )
    public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog( Bundle savedInstanceState ) {
            final Calendar c = Calendar.getInstance();
            int year = c.get( Calendar.YEAR );
            int month = c.get( Calendar.MONTH );
            int day = c.get( Calendar.DAY_OF_MONTH );
            return new DatePickerDialog( getActivity(), this, year, month, day );
        }

        public void onDateSet( DatePicker view, int selectedYear, int selectedMonth, int selectedDay ) {
            int year = selectedYear;
            int month = selectedMonth;
            int day = selectedDay;
            myEditText4 = (day + " / " + (month + 1) + " / " + year);

            return;
        }
    }

}

Solution

  • After debugging the state of all variables, I was able to figure out the issue. Here is the solution
    1) When I did Datebtn inside the onDateSet method, I was actually trying to get the Datebtn object from the Datepicker view. The Datebtn is not in this fragment and hence it gives null pointer exception
    2) I then attempted to get the parent fragment from inside the onDateSet using fragment manager object and findFragmentByTag("AFragment"). This is also returned a null pointer because "AFragment" tag is not defined for the AFragment layout xml or AFragment class
    3) Then I tried to get a valid AFragment object from fragment manager object using findFragmentByID (R.id.fragment_container). At this step, I got a valid AFragment object handle in DatePickerFragment class. I was then able to do setText on the Datebtn in AFragment class from DatePickerFragment class

    Here is how the working code for DatePickerFragment's onDateSet looks

        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            int year = selectedYear;
            int month = selectedMonth;
            int day = selectedDay;
            myEditText4 = (day + " / " + (month + 1) + " / " + year);
            FragmentManager fm = this.getFragmentManager();
            Fragment parFragment = fm.findFragmentById(R.id.fragment_container);
            Button dbtn = (Button) parFragment.getView().findViewById(R.id.datepickerbutton);
            dbtn.setText(myEditText4);
        return;
        }